SQL - 加入 2 个过滤和分组的时间序列
SQL - join 2 filtered and grouped time series
我有 2 个 SQL 查询,我正在尝试 运行 使用 Presto/AWS Athena,它们看起来有点像这样:
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,
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')
我想将这些结果合并到一个 table 中,基本上是一个时间序列,每个日期都有 2 个值。我希望 filtered_id
和 unfiltered_id
保持单独的列。我不确定如何实现这一点,我尝试加入同一个 table 但我无法弄清楚如何仅针对 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');
我有 2 个 SQL 查询,我正在尝试 运行 使用 Presto/AWS Athena,它们看起来有点像这样:
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,
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')
我想将这些结果合并到一个 table 中,基本上是一个时间序列,每个日期都有 2 个值。我希望 filtered_id
和 unfiltered_id
保持单独的列。我不确定如何实现这一点,我尝试加入同一个 table 但我无法弄清楚如何仅针对 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');