有人遞給我一個場景,具有尋找解決這個sql查詢
產品架構的更優雅的方式:
maker model type
A 1232 PC
A 1233 PC
A 1276 Printer
A 1298 Laptop
A 1401 Printer
A 1408 Printer
A 1752 Laptop
B 1121 PC
B 1750 Laptop
C 1321 Laptop
D 1288 Printer
D 1433 Printer
E 1260 PC
E 1434 Printer
E 2112 PC
E 2113 PC
和查詢是
獲取製造商只生產一種產品類型和多個 模型。
輸出列應該是製造商和類型。
這就是我想出來的。
select distinct maker, type
from Product
where maker in (select maker
from Product
group by maker, type
having count(model) > 1
except
select maker
from
(
select distinct maker, type
from Product
) A
group by maker
having count(type) > 1)
我知道這似乎不優雅的以任何方式,所以我想知道是否有人能想出一個更好的選擇和爲什麼它比上述的查詢更好地解釋。
編輯:請確保答案是僅僅兩列寬是
設備,類型
能否請你添加一個標籤爲您的RDBMS引擎? – amphetamachine
子查詢可以簡單地是一個GROUP BY,在HAVING中有條件。 – jarlh