繁体   English   中英

将row_number()拆分为多个列的分区

[英]split row_number() over partition over multiple columns

我有一个查询,该查询在分区上使用row_number()。 结果出来时看起来像

Product         Row_Number         Price
A               1                  25
A               2                  20
A               3                  15
B               1                  100
B               2                  10
B               3                  2

我想让结果显示在像

Product      Row1         Row2        Row3      price1       price2       price3
A            1            2           3         25           20           15
B            1            2           3         100          10           2

我应该使用rank()之类的东西吗???

我正在使用Teradata

您可以再添加两个窗口函数来获得第二和第三最高价格,该价格应与当前ROW_NUMBER的运行状态相同,因此不会产生额外的开销:

select
   product,
   price as Price1,
   min(price)
   over (partition by product
         order by price desc
         rows between 1 following and 1 following) as Price2,
   min(price)
   over (partition by product
         order by price desc
         rows between 2 following and 2 following) as Price3
from tab
qualify 
   row_number() 
   over (partition by product
         order by price desc) = 1

我只给每个排序参数指定排序方向,然后就可以了,很好。 不使用分区。

SELECT TOP (5) ROW_NUMBER() OVER (ORDER BY SCHEME ASC,APPLICATION_DATE DESC,TRANSACTION_REF_NO ASC,APPLICATION_STATUS DESC)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM