我有一张看起来像这样的表
Invoice |Line # |Item # |Price Per
1 |1 |11 |5.00
1 |2 |22 |10.00
2 |1 |11 |5.00
2 |2 |22 |12.00
3 |1 |11 |5.00
4 |1 |11 |6.00
我想获得一件商品的最后售价。
如何运行产生以下结果的脚本?
Invoice |Line # |Item # |Price Per
2 |2 |22 |12.00
4 |1 |11 |6.00
我使用此脚本与当前售价进行比较。
由于
答案 0 :(得分:0)
假设invoice
和line
定义了排序,那么传统方法使用row_number()
:
select t.*
from (select t.*,
row_number() over (partition by item order by invoice desc, line desc) as seqnum
from t
) t
where seqnum = 1;