Sql server 2008 带MAX函数的SQL连接

Sql server 2008 带MAX函数的SQL连接,sql-server-2008,Sql Server 2008,我已经在sql中创建了两个表 1.产品 与 它引用ProductCategory表的主键 2.产品类别 与 我想展示每个类别中价格最高的产品 假设有两类 1.Soap 2.Shampoo. 在产品表中有4行 1.Dove Soap with price 42Rs 2.Dettol Soap with price 25Rs 3.Dove Shampoo with price 120Rs and 4.Sunsilk Shampoo with Price 140Rs 那么输出应该是 1.Dove

我已经在sql中创建了两个表

1.产品

它引用ProductCategory表的
主键

2.产品类别

我想展示每个类别中价格最高的产品

假设有两类

1.Soap 
2.Shampoo.
在产品表中有4行

1.Dove Soap with price 42Rs
2.Dettol Soap with price 25Rs
3.Dove Shampoo with price 120Rs and
4.Sunsilk Shampoo with Price 140Rs
那么输出应该是

1.Dove肥皂,价格42,类别名称肥皂。 2.Sunsilk洗发水,价格140,品类名称洗发水

请使用联接操作回复此sql查询。

尝试此操作

;WITH MaxValues
AS
(
SELECT MAX(p.ProductPrice) MaxPrice, p.ProductCategoryId ProductCategoryId
FROM Product p 
GROUP BY p.ProductCategoryId
)
SELECT p.ProductName, m.MaxPrice, c.CategoryName
FROM MaxValues m JOIN Product p ON m.ProductCategoryId = p.ProductCategoryId
JOIN ProductCategory c ON p.ProductCategoryId = c.CategoryId
WHERE p.ProductPrice = m.MaxPrice

你问之前试过什么吗?也许你应该问问你的老师。一个类别是否有两个相同的最大值的项目?就像另一种肥皂,但它的价格也是42卢比?如果可能,您希望如何向他们展示?
1.Dove Soap with price 42Rs
2.Dettol Soap with price 25Rs
3.Dove Shampoo with price 120Rs and
4.Sunsilk Shampoo with Price 140Rs
;WITH MaxValues
AS
(
SELECT MAX(p.ProductPrice) MaxPrice, p.ProductCategoryId ProductCategoryId
FROM Product p 
GROUP BY p.ProductCategoryId
)
SELECT p.ProductName, m.MaxPrice, c.CategoryName
FROM MaxValues m JOIN Product p ON m.ProductCategoryId = p.ProductCategoryId
JOIN ProductCategory c ON p.ProductCategoryId = c.CategoryId
WHERE p.ProductPrice = m.MaxPrice