当日期和doc_Types相等时在表上执行自连接

时间:2019-01-04 13:57:50

标签: sql join amazon-athena presto

我想将同一天的doc_type分组在一起。例如,如果'1099'doc_type在2018-12-12上被分类了两次。我想对它们进行两次计数,并将频率显示为2。

这是我的查询。我应该在doc_type和date相等的条件下对表执行自我连接吗?

SELECT    cast(date_parse(date_time, '%Y/%m/%d %H:%i:%s') AS date) AS Date
        , json_array_get (doc_type, 0) AS doc_type
        , COUNT(DISTINCT doc_type) as Frequency 
FROM "reports"."dev_operator_logs" 
WHERE doc_type IS NOT NULL 
GROUP BY date_time, doc_type 
ORDER BY date_time;

Table looks like this

1 个答案:

答案 0 :(得分:0)

也许您只是想按date分组而不是datetime并删除doc_type

SELECT cast(date_parse(date_time, '%Y/%m/%d') AS date) AS Date,
       json_array_get(doc_type, 0) AS doc_type,
       COUNT(DISTINCT doc_type) as Frequency 
FROM "reports"."dev_operator_logs" 
WHERE doc_type IS NOT NULL 
GROUP BY cast(date_parse(date_time, '%Y/%m/%d') AS date) 
ORDER BY cast(date_parse(date_time, '%Y/%m/%d') AS date);