这是我的情景...... 多个项目类型具有多个价格的多个项目。 想要选择所有类型显示MAX价格的商品。无法弄清楚如何获得最大值?
输入:
ProductId ProductType Description Price
1 A BAKED BEANS 1.29
1 B BAKED BEANS 1.98
输出:
ProductId ProductType Description Price
1 A BAKED BEANS 1.98
1 B BAKED BEANS 1.98
有什么建议吗?
答案 0 :(得分:4)
试试这个:
SELECT ProductId,
ProductType,
Description,
b.price
FROM <YOUR_TABLE> a,
(SELECT MAX(price) price FROM <YOUR_TABLE>) b
对于那些喜欢ANSI语法的人:
SELECT ProductId,
ProductType,
Description,
b.price
FROM <YOUR_TABLE> a INNER JOIN
(SELECT MAX(price) price FROM <YOUR_TABLE>) b
ON 1=1
答案 1 :(得分:3)
Select ProductId, ProductType, Description, MaxByDesc.MaxPrice
From Product
Join (
Select Description, Max(Price) As MaxPrice
From Product
Group By Description
) As MaxByDesc
On MaxByDesc.Description = Product.Description
如果您使用的是SQL Server 2005或更高版本:
Select ProductId, ProductType, Description
, Max( Price ) Over ( Partition By Description ) As MaxPrice
From Product
答案 2 :(得分:1)
Select ProductId, ProductType, Description, MaxByDesc.MaxPrice
From Product
Join (
Select Description, Max(Price) As MaxPrice
From Product
Group By ProductType
) As MaxByDesc
On MaxByDesc.ProductType = Product.ProductType
答案 3 :(得分:0)
另一种方式:
SELECT ProductId
, ProductType
, Description
, ( SELECT MAX(price)
FROM Product pg
WHERE pg.Description = p.Description
) AS MaxPrice
FROM Product p