使用ROW_NUMBER / OVER / PARTITION返回值最小的行

时间:2017-09-05 09:30:18

标签: sql-server

我有这个样本表

TypeId CapId Source Product Term MilesPa Price
1      38423 ALD    CH      2    8000    1404.37
1      38423 NET    CH      2    8000    1824.39

我想为typeid / capid / source / product / term / milespa的每个组合返回价格最低的行。

我尝试了这个,但它从行号= 1返回上面的两行。

SELECT ROW_NUMBER() OVER(PARTITION BY typeid,capid,source,product,term,milespa ORDER BY price ASC) as RowNumber,
TypeId,CapId,Source,Product,Term,MilesPa
FROM BestPriceLookup

谢谢!

1 个答案:

答案 0 :(得分:0)

因为你不需要组合来源,

SELECT Row_number() 
        OVER( 
        partition BY typeid, capid, product, term, milespa 
        ORDER BY price ASC) AS RowNumber, 
    typeid, 
    capid,  
    Source,      
    product, 
    term, 
    milespa 
FROM   bestpricelookup