缺失日期的零填充数据

时间:2015-10-08 19:11:05

标签: sql netezza

我有两张桌子:

Cust    Sales   Week
123     4       1/8/2015
123     3       1/22/2015
234     4       1/1/2015

Week
1/1/2015
1/8/2015
1/15/2015
1/22/2015

我希望将它们组合起来,以便每个Cust都有每个日期,没有销售的地方就是0。

Cust    Sales   Week
123     4       1/1/2015
123     0       1/8/2015
123     0       1/15/2015
123     3       1/22/2015
234     4       1/1/2015
234     0       1/8/2015
234     0       1/15/2015
234     0       1/22/2015

有没有办法可以'选择不同的(Cust)'并以某种方式加入它们?

3 个答案:

答案 0 :(得分:1)

首先,使用cross join生成所需的行。然后使用left join

输入您想要的数据
select c.cust, w.week, coalesce(t.sales, 0) as sales
from weeks w cross join
     (select distinct cust from t) c left join
     t
     on t.cust = c.cust and t.week = w.week;

答案 1 :(得分:0)

您可以在日期表上left join并使用销售列上的isnull。在Netezza中使用等效的isnull

select t1.cust, isnull(t1.sales,0), t2.week
from daystable2 t2 left join salestable1 t1 on t1.week = t2.week

答案 2 :(得分:0)

我认为这会解决问题

SELECT week, cust, COALESCE(sales, 0)
FROM week_tbl a LEFT JOIN cust_table b
ON a.week = b.week
相关问题