【发布时间】:2021-07-22 20:13:00
【问题描述】:
我有一张如下所示的表格:
| 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 的文档,因此我尝试使用另一个表生成 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;
这行得通,但速度非常慢,而且丑陋。我想知道是否有更好的内置(或替代)方法来做到这一点。谢谢
【问题讨论】:
标签: sql date teradata missing-data