-1

SQLで2つのテーブルを作成しました....

1.製品

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

を指しprimary key of ProductCategory tableます。

2.製品カテゴリ

CategoryId as Primary Key and CategoryName.

カテゴリごとに最大価格の商品を表示したい.....

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.ダブソープ、価格42、カテゴリー名ソープ。2.サンシルクシャンプー、価格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 に答える