4

我有一个在分区上使用 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

4

2 回答 2

2

您可以再添加两个窗口函数来获得第二和第三高的价格,这应该与您当前的 ROW_NUMBER 在相同的 STAT 步骤中运行,因此没有额外的开销:

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
于 2013-09-26T17:29:38.850 回答
0

我只是为每个排序参数给出排序方向,然后,它就可以工作了,非常好。没有使用分区。

SELECT TOP (5) ROW_NUMBER() OVER (ORDER BY SCHEME ASC,APPLICATION_DATE DESC,TRANSACTION_REF_NO ASC,APPLICATION_STATUS DESC)
于 2015-05-05T05:03:38.243 回答