在 teradata 中传播缺失日期 - select 查询
Propagate missing dates in teradata - select query
我有一个 table 看起来像这样:
my_date
item_id.
sales
2020-03-01
GMZS72429
2
2020-03-07
GMZS72429
2
2020-03-09
GMZS72429
1
2020-03-04
GMZS72425
1
我希望它看起来像这样
my_date
item_id
sales
2020-03-01
GMZS72429
2
2020-03-02
GMZS72429
0
...
...
...
2020-03-05
GMZS72429
0
2020-03-06
GMZS72429
0
2020-03-07
GMZS72429
2
2020-03-08
GMZS72429
0
2020-03-09
GMZS72429
1
2020-03-01
GMZS72425
0
2020-03-02
GMZS72425
0
2020-03-03
GMZS72425
0
2020-03-04
GMZS72425
1
...
...
...
2020-03-09
GMZS72425
0
由于我一直在努力使用 Teradata 的文档,我尝试使用另一个 table 生成对 item_id - my_date
,然后是左连接:
with a1 as(
select distinct my_date, item_id from some_table_with_the_item_ids_and_all_dates
)
select a1.my_date, a1.item_id, coalesce(sales, 0) as sales
from a1 left join my_table on a1.item_id=my_table.item_id and a1.my_date=my_table.my_date;
这行得通,但速度非常慢,而且很难看。我想知道是否有更好的内置(或替代)方法来执行此操作。谢谢
一个简单的选择是使用 Teradata 的内置日期视图作为驱动程序:
select
coalesce(v.my_date,c.calendar_date),
item_id,
coalesce(v.sales,0)
from
sys_calendar.calendar c
left join your_table v
on v.my_date = c.calendar_date
where
c.calendar_date between (select min(my_date) from your_table ) and (select max(my_date) from your_table)
order by 1
这是 Teradata 的 EXPAND ON 语法的用例:
select
new_date
,item_id
,case when my_date = new_date then sales else 0 end
from
(
select dt.*, begin(p2) as new_date
from
(
select t.*
-- create a period for expansion in the next step
,period(my_date, lead(my_date, 1, my_date+1)
over (partition by item_id
order by my_date)) as pd
from vt as t
) as dt
-- now create the missing dates
expand on pd as p2
) as dt
我有一个 table 看起来像这样:
my_date | item_id. | sales |
---|---|---|
2020-03-01 | GMZS72429 | 2 |
2020-03-07 | GMZS72429 | 2 |
2020-03-09 | GMZS72429 | 1 |
2020-03-04 | GMZS72425 | 1 |
我希望它看起来像这样
my_date | item_id | sales |
---|---|---|
2020-03-01 | GMZS72429 | 2 |
2020-03-02 | GMZS72429 | 0 |
... | ... | ... |
2020-03-05 | GMZS72429 | 0 |
2020-03-06 | GMZS72429 | 0 |
2020-03-07 | GMZS72429 | 2 |
2020-03-08 | GMZS72429 | 0 |
2020-03-09 | GMZS72429 | 1 |
2020-03-01 | GMZS72425 | 0 |
2020-03-02 | GMZS72425 | 0 |
2020-03-03 | GMZS72425 | 0 |
2020-03-04 | GMZS72425 | 1 |
... | ... | ... |
2020-03-09 | GMZS72425 | 0 |
由于我一直在努力使用 Teradata 的文档,我尝试使用另一个 table 生成对 item_id - my_date
,然后是左连接:
with a1 as(
select distinct my_date, item_id from some_table_with_the_item_ids_and_all_dates
)
select a1.my_date, a1.item_id, coalesce(sales, 0) as sales
from a1 left join my_table on a1.item_id=my_table.item_id and a1.my_date=my_table.my_date;
这行得通,但速度非常慢,而且很难看。我想知道是否有更好的内置(或替代)方法来执行此操作。谢谢
一个简单的选择是使用 Teradata 的内置日期视图作为驱动程序:
select
coalesce(v.my_date,c.calendar_date),
item_id,
coalesce(v.sales,0)
from
sys_calendar.calendar c
left join your_table v
on v.my_date = c.calendar_date
where
c.calendar_date between (select min(my_date) from your_table ) and (select max(my_date) from your_table)
order by 1
这是 Teradata 的 EXPAND ON 语法的用例:
select
new_date
,item_id
,case when my_date = new_date then sales else 0 end
from
(
select dt.*, begin(p2) as new_date
from
(
select t.*
-- create a period for expansion in the next step
,period(my_date, lead(my_date, 1, my_date+1)
over (partition by item_id
order by my_date)) as pd
from vt as t
) as dt
-- now create the missing dates
expand on pd as p2
) as dt