如何在数据集中重复一个值,直到出现下一个值-SQL(Teradata)
我有一个基于在某些日子变化的产品和某些每天更改价值的产品的数据集。无论客户如何购买该产品直到其变化之日。因此,当我浏览数据时,看起来像这样 例如:
+---------+-------+------------+
| Product | Value | Date |
+---------+-------+------------+
| B | 5 | 21/05/2022 |
| A | 1 | 27/05/2022 |
| B | 2 | 28/05/2022 |
| C | 3 | 27/05/2022 |
| C | 4 | 28/05/2022 |
| A | 7 | 29/05/2022 |
| C | 5 | 29/05/2022 |
+---------+-------+------------+
我试图将其纳入这种格式:
+------------+---+---+---+
| Date | A | B | C |
+------------+---+---+---+
| 27/05/2022 | 1 | 5 | 3 |
| 28/05/2022 | 1 | 2 | 4 |
| 29/05/2022 | 7 | 2 | 5 |
+------------+---+---+---+
在Teradata SQL中做到这一点的最好方法是什么 (请注意,示例有点小,这可能是我重复某些产品的最低限度是7天)
I have a dataset based on products that change on certain days and some products that change value daily.However its possible for customers to purchase that product up until the date it changes. So when I pull through the data it looks like this
EG:
+---------+-------+------------+
| Product | Value | Date |
+---------+-------+------------+
| B | 5 | 21/05/2022 |
| A | 1 | 27/05/2022 |
| B | 2 | 28/05/2022 |
| C | 3 | 27/05/2022 |
| C | 4 | 28/05/2022 |
| A | 7 | 29/05/2022 |
| C | 5 | 29/05/2022 |
+---------+-------+------------+
I am trying to get it into this format:
+------------+---+---+---+
| Date | A | B | C |
+------------+---+---+---+
| 27/05/2022 | 1 | 5 | 3 |
| 28/05/2022 | 1 | 2 | 4 |
| 29/05/2022 | 7 | 2 | 5 |
+------------+---+---+---+
Whats the best way to do this in Teradata SQL
(note the example is a bit small, its likely the minimum i would need to repeat certain products is 7 days)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用
pivot
例如You could try the use of
pivot
e.g.在所有日期(或至少包括前X天/周)上旋转,以获取诸如'b'之类的产品的行),Apppy last_value忽略每个产品上的nulls,然后过滤日期范围。
但是,使用老式的最大值(情况)可能会得到一个更好的计划,并且在需要时动态创建更容易。
Pivot over all dates (or at least include the previous x days/weeks to get rows for products like 'B'), apppy LAST_VALUE IGNORE NULLS on each product and then filter the range of dates.
But using old-style MAX(CASE) will probably get a slightly better plan and it's easier create dynamically if needed.