如何在Looker中匹配顺序不同的组合字符串?

问题描述 投票:0回答:1

我有一个名为 国家/地区组合 的列,旁边的 总计数 列显示了该国家/地区组合出现的次数。我正在尝试在 Looker 中创建一个饼图来查看整体发生情况。

国家组合 总数
加拿大 - 印度 11
印度 - 加拿大 25

由于顺序原因,国家对出现了 2 次,我想将其合并为一个国家对,所以最后它变成加拿大-印度,总计数变为 36(11+25)

表中有几个这样的国家/地区对,我如何将其加入到一个唯一的对中以查看不重复的饼图。

我在自定义字段中尝试了 Looker 的 Group 选项,但我必须手动对所有组合进行分组,这是不可能的,因为组合不断变化并且有无数对。 我们如何将它们加入 Lookml 或 Looker 中?

visualization data-analysis pie-chart looker-studio looker
1个回答
0
投票

您应该创建一个专用的

country_combination_ordered
维度,在其中对国家/地区重新排序。它的sql参数是:

  ( 
    SELECT
      STRING_AGG(country_combinations, " - " ORDER BY country_combinations ASC)
      FROM UNNEST(split(country_combination, ' - '))  AS country_combinations
  )

您可以使用以下查询测试结果:

WITH Words AS
 (SELECT 'Canada - India' as country_combination, 11 as total_count UNION ALL
  SELECT 'India - Canada', 25)
SELECT
  ( 
    SELECT
      STRING_AGG(country_combinations, " - " ORDER BY country_combinations ASC)
      FROM UNNEST(split(country_combination, ' - '))  AS country_combinations
  ) AS country_combination_ordered,
  SUM(total_count) AS total_count
FROM Words
GROUP BY 1 
© www.soinside.com 2019 - 2024. All rights reserved.