-1

我在sql中创建了两个表......

1.产品

ProductId as Primary key,
ProductName,ProductPrice 
and ProductCategoryId as Foreign Key 

它指的是primary key of ProductCategory table.

2.产品类别

CategoryId as Primary Key and CategoryName.

我想在每个类别中显示具有最高价格的产品.....

假设有两个类别..

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 查询。

4

1 回答 1

0

尝试这个

;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
于 2013-03-25T03:08:25.590 回答