问题描述
我有2个要与Presto / AWS Athena一起运行的SQL查询,它们看起来像这样:
SELECT count(distinct id) as filtered_id,date_format(from_iso8601_timestamp(mydate),'%Y-%c') AS month_year
FROM table
WHERE value = 'bla'
GROUP BY date_format(from_iso8601_timestamp(mydate),'%Y-%c')
ORDER BY date_parse(month_year,'%Y-%c')
SELECT count(distinct id) as unfiltered_id,'%Y-%c') AS month_year
FROM table
GROUP BY date_format(from_iso8601_timestamp(mydate),'%Y-%c')
我想将这些结果合并到一个表中,基本上是一个时间序列,其中各个日期有2个值。我希望filtered_id
和unfiltered_id
保持单独的列。我不确定如何实现此目的,我尝试加入同一张表,但我不知道如何仅针对1个系列进行过滤。
基本上,我想要这样的结果:
filtered_ids,unfiltered_ids,month_year
6,15,2020-06
10,10,2020-07
10,20,2020-08
解决方法
如果我理解正确,则需要条件聚合:
SELECT count(distinct case when value = 'bla' then id end) as filtered_id,count(distinct id) as filtered_id,date_format(from_iso8601_timestamp(mydate),'%Y-%c') AS month_year
FROM table
GROUP BY date_format(from_iso8601_timestamp(mydate),'%Y-%c')
ORDER BY date_parse(month_year,'%Y-%c');