如何选择每个类别的最高价格的产品?

时间:2012-09-11 09:12:08

标签: sql sql-server database tsql

以下是我的表,其中包含以下项目:

ProductId       ProductName Category        Price
      1            Tiger      Beer          $12.00
      2             ABC       Beer          $13.99
      3            Anchor     Beer          $9.00
      4            Apolo      Wine          $10.88
      5           Randonal    Wine          $18.90
      6            Wisky      Wine          $30.19
      7             Coca     Beverage       $2.00
      8            Sting     Beverage       $5.00
      9             Spy      Beverage       $4.00
     10           Angkor      Beer          $12.88

我想我在这个表中只有三个category(我可以在这个表中有很多category)。我想在此表中显示每个category的最高产品价格。

7 个答案:

答案 0 :(得分:13)

如果你想获得整行,请试试这个,

支持大多数RDBMS

SELECT  a.*
FROM    tbProduct a
        INNER JOIN
        (
            SELECT Category, MAX(Price) maxPrice
            FROM tbProduct
            GROUP BY Category
        ) b ON a.category = b.category AND
                a.price = b.maxPrice

如果您使用MSSQL 2008+

WITH allProducts AS
(
SELECT  ProductId,ProductName,Category,Price,
        ROW_NUMBER() OVER (PARTITION BY CATEGORY ORDER BY Price DESC) ROW_NUM
FROM tbProduct
)
SELECT ProductId,ProductName,Category,Price
FROM allProducts
WHERE ROW_NUM = 1

SELECT ProductId,ProductName,Category,Price
FROM    
(
SELECT  ProductId,ProductName,Category,Price,
        ROW_NUMBER() OVER (PARTITION BY CATEGORY ORDER BY Price DESC) ROW_NUM
FROM tbProduct
) allProducts
WHERE ROW_NUM = 1

SQLFiddle Demo

答案 1 :(得分:4)

SELECT   Category,max(Price) as Price
FROM     tbProduct
GROUP BY Category

如果您想要检索其他字段以及类别名称,请:

select * 
from  tbProduct T
join   (
         select Category,max(Price) as Price
         from tbProduct
         group by Category)a
on     T.Category=a.Category
and    T.Price=a.Price

答案 2 :(得分:2)

这应该有效:

SELECT Category, MAX(Price)
FROM Products
GROUP BY Category

这会按每个类别对表格进行分组,并使用MAX汇总功能获得每个类别的最高价格。

答案 3 :(得分:2)

select *
from
(Select *, 
        row_number() over (partition by category order by price desc) rn 
        from products) v
where rn=1

答案 4 :(得分:1)

这是另一种方法,其中MAX被视为分析函数(自SQL Server 2005以来可用)

    Declare @t table(ProductId  int identity, ProductName varchar(100), Category varchar(20), Price numeric(10,2))
    Insert Into @t Select 'Tiger','Beer',12.00
    Insert Into @t Select 'ABC','Beer',13.99
    Insert Into @t Select 'Anchor','Beer',9.00
    Insert Into @t Select 'Apolo','Wine',10.88
    Insert Into @t Select 'Randonal','Wine',18.90
    Insert Into @t Select 'Wisky','Wine',30.19
    Insert Into @t Select 'Coca','Beverage',2.00
    Insert Into @t Select 'Sting','Beverage',5.00
    Insert Into @t Select 'Spy','Beverage',4.00
    Insert Into @t Select 'Angkor','Beer',12.88

SELECT ProductId,ProductName,Category,Price
FROM(
        SELECT
            ProductId
            ,ProductName
            ,Category
            ,Price
            ,CASE WHEN Price =  MAX(Price) OVER(PARTITION BY Category ORDER BY (SELECT 1)) THEN 'Take' ELSE 'Leave' END AS PickUp   

        FROM @t

        )X  WHERE PickUp = 'Take' ORDER BY 1

<强>结果

ProductId   ProductName Category    Price
2           ABC         Beer            13.99
6           Wisky       Wine            30.19
8           Sting       Beverage        5.00

答案 5 :(得分:1)

SELECT   Category,max(Price) as ProdPrice
FROM     tbProduct
GROUP BY Category

答案 6 :(得分:0)

SELECT TOP(7)*,
             Row_number()
               OVER (
                 partition BY category
                 ORDER BY price DESC) rn
FROM   products
ORDER  BY price DESC