如何在sql server 2008中找到最低销售价格记录?

时间:2010-09-01 09:53:39

标签: sql-server sql-server-2008

ProductPrice table:
ProductPriceId,ProductId,CurrencyId,CustomerGroupId,PriceTierId,List,Selling,Bulk   



868      1      1      NULL   NULL      45.00      42.00      42.00      
869      1      1      2      NULL      39.00      36.00      33.00      
870      1      3      NULL   NULL      48.00      45.00      45.00      
871      1      1      5      NULL      40.00      40.00      40.00      
872      2      1      NULL   NULL      50.00      48.00      48.00      
873      2      3      NULL   NULL      50.00      50.00      50.00      
874      2      1      2      NULL      45.00      45.00      45.00      
875      2      1      5      NULL      56.00      56.00      56.00      

产品ID有4条记录我想在表中找到最低销售价格记录。 例如
  产品ID = 1   结果是:

869 1   1   2   NULL    39.00   36.00   33.00   

请帮帮我

4 个答案:

答案 0 :(得分:0)

如果出现平局,以下内容将返回多行。如果您不希望使用Row_number()代替Rank()

;WITH cte AS
     ( SELECT  ProductPriceId,
              ProductId      ,
              CurrencyId     ,
              CustomerGroupId,
              PriceTierId    ,
              List           ,
              Selling        ,
              Bulk           ,
              RANK() OVER (PARTITION BY ProductId ORDER BY Selling) AS Rnk
     FROM     ProductPrice
     )
SELECT ProductPriceId ,
       ProductId      ,
       CurrencyId     ,
       CustomerGroupId,
       PriceTierId    ,
       List           ,
       Selling        ,
       Bulk
FROM   cte
WHERE  Rnk=1

答案 1 :(得分:0)

SELECT TOP 1 
    * 
FROM 
    ProductPrice 
WHERE 
    ProductId = 1 
ORDER BY 
    Selling ASC

答案 2 :(得分:0)

select t1.*
from ProductPrice t1
where not exists(select *
                 from ProductPrice t2
                 where t2.Selling<t1.Selling and t2.productid=t1.priductid)

这将返回每个产品ID的所有最低销售价格记录。如果有多个这样的行,那么它将返回多个。

答案 3 :(得分:0)

您可以使用以下查询来查找所需的结果。

SELECT  ProductPriceId, ProductId, CurrencyId, CustomerGroupId, PriceTierId, List, Selling, Bulk,

        RANK() OVER (PARTITION BY ProductId ORDER BY Selling DESC) AS Rnk

FROM    ProductPrice

WHERE  Rnk=1