如何在Redshift中强制零值?

时间:2018-01-24 03:05:04

标签: sql amazon-redshift

如果这是我的查询:

select
 (min(timestamp))::date as date,
 (count(distinct(user_id)) as user_id_count
 (row_number() over (order by signup_day desc)-1) as days_since
from
  data.table
where
  timestamp >= current_date - 3
group by
  timestamp
order by
  timestamp asc;

这些是我的结果

date        | user_id_count   | days_since
------------+-----------------+-------------
2018-01-22  | 3               | 1
2018-01-23  | 5               | 0

如何才能显示要显示的表(用户ID计数为0?):

date        | user_id_count   | days_since
------------+-----------------+-------------
2018-01-21  | 0               | 0
2018-01-22  | 3               | 1
2018-01-23  | 5               | 0

2 个答案:

答案 0 :(得分:2)

您需要生成日期。在Postgres中,generate_series()是要走的路:

select g.ts as dte,
       count(distinct t.user_id) as user_id_count
       row_number() over (order by signup_day desc) - 1) as days_since
from generate_series(current_date::timestamp - interval '3 day', current_date::timestamp, interval '1 day') g(ts) left join
     data.table t
     on t.timestamp::date = g.ts
group by t.ts
order by t.ts;

答案 1 :(得分:0)

您必须创建一个包含日期的“日历”表,然后左键加入您的汇总结果:

with 
aggregated_result as (
    select ...
)
select
 t1.date
,coalesce(t2.user_id_count,0) as user_id_count
,coalesce(t2. days_since,0) as days_since
from calendar t1
left join aggregated_result t2
using (date)

有关创建日历表的更多信息:How do I create a dates table in Redshift?