我有一张看起来像下面的桌子
| id | views | actions |
|----|---------------------------------------------------------------------------|---------------------------------------------------------------------------|
| 1 | {"group1":{"mobile":20,"desktop":36},"group2":{"mobile":25,"desktop":41}} | {"group1":{"mobile":4,"desktop":3},"group2":{"mobile":2,"desktop":4}} |
| 2 | {"group1":{"mobile":40,"desktop":34},"group2":{"mobile":31,"desktop":45}} | {"group1":{"mobile":21,"desktop":19},"group2":{"mobile":23,"desktop":19}} |
| 3 | {"group1":{"mobile":53,"desktop":34},"group2":{"mobile":37,"desktop":75}} | {"group1":{"mobile":17,"desktop":14},"group2":{"mobile":30,"desktop":12}} |
我希望我的结果看起来像下面的
| product | views group 1 | views group 2 | actions group 1 | actions group 2 | actions per view group 1 | actions per view group 2 |
|-------------|----------------|----------------|-----------------|-----------------|--------------------------|--------------------------|
| 1 (mobile) | 20 | 25 | 4 | 2 | (D2/B2) = 0.2 | (E2/C2) = 0.08 |
| 1 (desktop) | 36 | 41 | 3 | 4 | (D3/B3) = 0.115 | (E2/C2) = 0.09 |
| 2 (mobile) | 40 | 31 | 21 | 23 | ... | ... |
| 2 (desktop) | 34 | 45 | 19 | 19 | ... | ... |
| 1 (overall) | (B2 + B3) = 56 | (C2 + C3) = 66 | ... | ... | ... | ... |
| 2 (overall) | (B4 + B5) = 84 | (C4 + C5) = 76 | ... | ... | ... | ... |
我可以使用以下SQL查询来执行此操作,但是我想知道是否可以以更简洁或更短的方式进行操作
SELECT
CASE id WHEN 1 THEN '1 (mobile)' WHEN 2 THEN '2 (mobile)' END as product,
SUM(json_extract_path_text(actions, 'group1', 'mobile') AS actions_group_1,
SUM(json_extract_path_text(views, 'group1', 'mobile') AS views_group_1,
SUM(json_extract_path_text(actions, 'group2', 'mobile') AS actions_group_2,
SUM(json_extract_path_text(views, 'group2', 'mobile') AS views_group_2,
SUM(json_extract_path_text(actions, 'group1', 'mobile')/SUM(json_extract_path_text(views, 'group1', 'mobile') AS actions_per_view_group_1 ,
SUM(json_extract_path_text(actions, 'group2', 'mobile')/SUM(json_extract_path_text(views, 'group2', 'mobile') AS actions_per_view_group_2
FROM widget_daily_stats
WHERE id IN (1,2)
GROUP BY id
UNION
SELECT
CASE id WHEN 1 THEN '1 (desktop)' WHEN 2 THEN '2 (desktop)' END as product,
SUM(json_extract_path_text(actions, 'group1', 'desktop') AS actions_group_1,
SUM(json_extract_path_text(views, 'group1', 'desktop') AS views_group_1,
SUM(json_extract_path_text(actions, 'group2', 'desktop') AS actions_group_2,
SUM(json_extract_path_text(views, 'group2', 'desktop') AS views_group_2,
SUM(json_extract_path_text(actions, 'group1', 'desktop')/SUM(json_extract_path_text(views, 'group1', 'desktop') AS actions_per_view_group_1 ,
SUM(json_extract_path_text(actions, 'group2', 'desktop')/SUM(json_extract_path_text(views, 'group2', 'desktop') AS actions_per_view_group_2
FROM widget_daily_stats
WHERE id IN (1,2)
GROUP BY id
UNION
...
我必须有一种方法可以遍历移动设备和台式机并动态地统一结果,而不必显式地统一所有结果
答案 0 :(得分:0)
首选解决方案是不要在Amazon Redshift中将数据存储为JSON 。如您所见,您需要使用json_extract_path_text()
函数来检索单个元素,这很慢。
此外,将字段存储在JSON中意味着Redshift无法优化存储和访问。如果数据存储在单独的字段中(如您在输出中显示的那样),则Redshift效率会更高。
因此:
很抱歉,此答案并未专门解决您的UNION问题,但您会发现将数据转换为更可用的格式将有助于您查询数据。