2013-10-07 46 views
1

我有以下數據的命名產品表:我想檢索表,其中列有相同的數據

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 

我想要檢索機和鍵入

條件1:誰只生產車型屬於同一類型。
條件2:這些模型的數目超過1所需

結果:

maker type 
D  Printer 

回答

3

要獲得製造者:

select distinct maker 
from your_table 
group by maker 
having count(distinct type) = 1 
and count(*) > 1 

要獲得壺和類型:

select distinct t.maker, t.type 
from your_table t 
inner join 
(
    select maker 
    from your_table 
    group by maker 
    having count(distinct type) = 1 
    and count(*) > 1 
) x on x.maker = t.maker 
+0

行返回兩次 –

+1

使用'選擇distinct' –

2

請嘗試:

select 
    distinct Maker, Type 
from(
    select 
     *, 
     COUNT(*) over (partition by Maker, type) TypeCnt, 
     COUNT(*) over (partition by Maker) MakerCnt 
    from YourTable 
)x where TypeCnt=MakerCnt and TypeCnt>1 
1
SELECT types.[maker], types.[type] FROM 
(SELECT [maker], COUNT([model]) as cnt 
FROM Table1 
GROUP BY [maker]) makers 
INNER JOIN 
(SELECT [maker], [type], COUNT([model]) as cnt 
FROM Table1 
GROUP BY [maker], [type]) types 
ON makers.[maker] = types.[maker] 
AND makers.cnt = types.cnt 
AND types.cnt > 1 

SAMPLE FIDDLE

1
SELECT maker, MIN(type) AS type 
FROM product 
GROUP BY maker 
HAVING MIN(type) = MAX(type) 
    AND COUNT(DISTINCT model) > 1 ; 
相關問題