格式化具有多种条件的数据透视表

时间:2019-09-26 11:33:58

标签: sql amazon-web-services amazon-athena

我当前的查询是

Failed to create a service

Tracking number: d123456789

现在,它显示的输出具有以下格式:

  

用户|日期|事件
   3 | 2019-09-25 |访问|
   4 | 2019-09-25 | click_button |
   10 | 2019-09-25 |访问|

我希望它的格式为

  

|日期| users_visits | users_clicks |
  | 2019-09-25 | 3 | 4 |
  | 2019-09-26 | 10 | 0 |

我编写了此查询,但是它不起作用,我真的不明白如何在透视查询中正确获取计数查询

SELECT COUNT (DISTINCT("json_extract_scalar"("data", '$.user_id'))) AS users, event, date(timestamp)
FROM tableName
WHERE category='category'
GROUP BY event, date(timestamp)
ORDER BY date(timestamp) DESC

1 个答案:

答案 0 :(得分:0)

条件聚合起作用吗?

SELECT date(timestamp),
       COUNT(DISTINCT CASE WHEN event = 'visit'
                           THEN "json_extract_scalar"("data", '$.user_id')
                      END) as visits,
       COUNT(DISTINCT CASE WHEN event = 'click_button'
                           THEN "json_extract_scalar"("data", '$.user_id')
                      END) as click_button
FROM tableName
WHERE category = 'category'
GROUP BY date(timestamp)
ORDER BY date(timestamp) DESC