【发布时间】:2021-08-04 21:41:47
【问题描述】:
我有这张桌子:
| Date | ID | Product_A_Count | Product_B_Count |
|---|---|---|---|
| 1 | A | 1 | 2 |
| 2 | A | 1 | 2 |
| 3 | A | 2 | 1 |
| 4 | A | 1 | 2 |
我想把它转换成:
| Start_Date | End_Date | ID | Product_A_Count | Product_B_Count |
|---|---|---|---|---|
| 1 | 2 | A | 1 | 2 |
| 3 | 3 | A | 2 | 1 |
| 4 | 4 | A | 1 | 2 |
有没有办法只使用 SQL 来做到这一点?我一直在尝试 row_number 和 lag 但没有一个看起来正确(因为按 ID、Product_A_Count 和 Product_B_Count 组合的分区不是唯一的)。
非常感谢所有建议!
【问题讨论】:
标签: sql window-functions