我有一个数据集,我试图在teradata中生成一个序列号 在下面的数据集中,订单(550)在商店中进行了多次修改(共10个) 最后一列ShopSequence是我想要实现的,当订单被移动到另一个商店时,ShopSequence数字增加1 因此,该数据集读取了4个商店的10个修改 有很多这样的订单
Order, Shop, SequenceNumber, ShopSequence
550, AA, 1 1
550, AA, 2 1
550, AA, 3 1
550, BB, 4 2
550, BB, 5 2
550, BB, 6 2
550, BB, 7 2
650, CC, 8 3
650, CC, 9 3
650, CC, 10 3
我尝试了许多OLAP配置,但不幸的是我无法让它工作
ROW_NUMBER() OVER (ORDER BY SequenceNumber asc, Shop asc) AS shop_row_number
,
RANK() OVER (PARTITION BY Shop asc ORDER BY SequenceNumber ASC
感谢您的时间
答案 0 :(得分:2)
尝试使用DENSE_RANK OVER (ORDER BY Shop asc)
。
只要没有第二组行具有相同的shop
值,就可以正常工作。
编辑:
根据您的评论值可能会重复,因此这也适用于重复的组,但需要嵌套的OLAP函数:
select ...
sum(flag) -- calculate the rank
over (partition by Order
order by SequenceNumber
rows unbounded preceding) as ShopSequence
from
(
select ...
-- flag the rows where a new group starts
-- 'min...' can be replaced by LAG in TD16.10+
case when min(Shop)
over (partition by Order
order by SequenceNumber
rows between 1 preceding and 1 preceding) = Shop
then 0
else 1
end as flag
) as dt