如何在数据集中重复一个值,直到出现下一个值-SQL(Teradata)

发布于 2025-02-02 11:40:09 字数 769 浏览 3 评论 0原文

我有一个基于在某些日子变化的产品和某些每天更改价值的产品的数据集。无论客户如何购买该产品直到其变化之日。因此,当我浏览数据时,看起来像这样 例如:

+---------+-------+------------+
| 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅淡淡 2025-02-09 11:40:09

您可以尝试使用pivot例如

SEL date, a, b, c
FROM your_table 
PIVOT (
    MAX(value)
    FOR product IN ('a','b','c')
) piv;

You could try the use of pivot e.g.

SEL date, a, b, c
FROM your_table 
PIVOT (
    MAX(value)
    FOR product IN ('a','b','c')
) piv;
护你周全 2025-02-09 11:40:09

在所有日期(或至少包括前X天/周)上旋转,以获取诸如'b'之类的产品的行),Apppy last_value忽略每个产品上的nulls,然后过滤日期范围。

with cte as
 (
   select 
      date
     ,last_value(a ignore nulls) over (order by date) as a
     ,last_value(b ignore nulls) over (order by date) as b
     ,last_value(c ignore nulls) over (order by date) as c
   from tab
   PIVOT (
       MAX(value_)
       FOR product IN ('a' as a
                      ,'b' as b
                      ,'c' as c)
   ) as pvt
 )
select * 
from cte   
where date between date '2022-05-27' 
               and date '2022-05-29'

但是,使用老式的最大值(情况)可能会得到一个更好的计划,并且在需要时动态创建更容易。

select 
   date
  ,last_value(max(case when product = 'a' then value end) ignore nulls) over (order by date)
  ,last_value(max(case when product = 'b' then value end) ignore nulls) over (order by date)
  ,last_value(max(case when product = 'c' then value end) ignore nulls) over (order by date)
from tab
group by 1
qualify date between date '2022-05-27'
                 and date '2022-05-29'

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.

with cte as
 (
   select 
      date
     ,last_value(a ignore nulls) over (order by date) as a
     ,last_value(b ignore nulls) over (order by date) as b
     ,last_value(c ignore nulls) over (order by date) as c
   from tab
   PIVOT (
       MAX(value_)
       FOR product IN ('a' as a
                      ,'b' as b
                      ,'c' as c)
   ) as pvt
 )
select * 
from cte   
where date between date '2022-05-27' 
               and date '2022-05-29'

But using old-style MAX(CASE) will probably get a slightly better plan and it's easier create dynamically if needed.

select 
   date
  ,last_value(max(case when product = 'a' then value end) ignore nulls) over (order by date)
  ,last_value(max(case when product = 'b' then value end) ignore nulls) over (order by date)
  ,last_value(max(case when product = 'c' then value end) ignore nulls) over (order by date)
from tab
group by 1
qualify date between date '2022-05-27'
                 and date '2022-05-29'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文