2017-08-10 91 views
0

(使用Oracle SQLDeveloper) 我需要生成一條SQL語句,該語句將查找形狀爲正方形但不是圓形的顏色數量。SQL選擇不同的COL1,其中COL2是_且COL2從不_ _?

下表的標題是:MYOBJECTS

目前我的SQL語句(這是不完全)是這樣的:

SELECT DISTINCT COLOR, SHAPE FROM MYOBJECTS WHERE ((SHAPE = 'square') AND (..........)); 

enter image description here

僅供參考答案應該是:

顏色 | SHAPE
yellow |平方
棕色|廣場

任何人都可以幫忙?

回答

2

嗯。 。 。一種方法是兩級聚合。另一種選擇是使用子查詢:

SELECT COUNT(DISTINCT COLOR) 
FROM MYOBJECTS o 
WHERE SHAPE = 'square' AND 
     NOT EXISTS (SELECT 1 
        FROM MYOBJECTS o2 
        WHERE o2.color = o.color AND o2.SHAPE = 'circle' 
       ); 

編輯:

哦,我注意到,你要在各個行,不計:

SELECT DISTINCT COLOR, SHAPE 
FROM MYOBJECTS o 
WHERE SHAPE = 'square' AND 
     NOT EXISTS (SELECT 1 
        FROM MYOBJECTS o2 
        WHERE o2.color = o.color AND o2.SHAPE = 'circle' 
       ); 
0

我想你只需要GROUP BY

SELECT COLOR, SHAPE 
FROM MYOBJECTS 
WHERE SHAPE = 'square' 
GROUP BY COLOR 
1

其他group by答案是不正確的,但心理鍛鍊爲什麼會t阿克與羣組做...

SELECT color FROM MYOBJECTS GROUP BY color HAVING SUM(shape = 'square') > 0 AND SUM(shape = 'circle') = 0

相關問題