找出只生产相同型号的制造商的制造商 这些模型的数量超过1. Deduce:maker,type
表格信息:
产品(制造商,型号,类型)
PC(代码,型号,速度,内存,高清,CD,价格)
笔记本电脑(代码,型号,速度,内存,高清,屏幕,价格)打印机(代码, 型号,颜色,类型,价格)
我写的版本(通过在我的jotta pad上抓第一个lolz):
SELECT Product.maker, Product.type
FROM Product
WHERE Product.maker IN
(SELECT X.maker
FROM
(SELECT DISTINCT maker,type
FROM Product
) AS X
GROUP BY X.maker
HAVING COUNT(X.type) = 1
)
GROUP BY Product.maker,Product.type
HAVING COUNT(Product.model) > 1
这给了我正确的结果。但是,我相信这可能不是唯一的解决方案(至少不是理想的解决方案) - 因为我不是SQL向导。
我很感激对答案的任何解释,但如果我更好地简化它,我将使用新的查询语句更新问题。
TA
更新 自从发布以来,我将其概括为:
SELECT maker, type
FROM Product
WHERE Product.maker IN
(
SELECT maker
FROM Product
GROUP BY maker
HAVING COUNT(DISTINCT type)=1
)
GROUP BY maker, type
HAVING COUNT(product.model) > 1
答案 0 :(得分:1)
可以简化:
SELECT maker ,
MAX(type) AS type
FROM product
GROUP BY maker
HAVING COUNT(DISTINCT type) = 1
AND COUNT(*) > 1
在这里,您按制造商对结果进行分组,并且您只需要那些不同类型计数为1且组中总行数大于1的组。
你可以写:
HAVING COUNT(DISTINCT type) = 1
AND COUNT(DISTINCT model) > 1
但它与It is assumed that model numbers in the Product table are unique for all the makers and product types
相同。