2012-07-27 69 views
2

我在Oracle DB中具有下表。SQL檢查列中的所有值

ID VALUE 
----------- 
1 1 
1 2 
1 3 
2 1 
2 2 
3 1 
3 2 
3 3 
4 1 

如何選擇的ID裏面有所有3個值(1,2,3)

+0

值字段是否包含1,2,3以外的任何內容? – hmmftg 2012-07-27 04:16:52

+0

標題是** SQL檢查列**或** SQL中的所有值以檢查列**中的特定值? – hmmftg 2012-07-27 04:18:27

回答

2

試試這個:

SELECT ID 
    FROM TABLENAME T 
WHERE EXISTS (SELECT * 
       FROM TABLENAME T1 
       WHERE T1.ID = T.ID AND T1.VALUE = '1') 
    AND EXISTS (SELECT * 
       FROM TABLENAME T2 
       WHERE T1.ID = T.ID AND T2.VALUE = '2') 
    AND EXISTS (SELECT * 
       FROM TABLENAME T3 
       WHERE T1.ID = T.ID AND T2.VALUE = '3') 

SELECT ID 
    FROM TABLENAME T 
WHERE (SELECT COUNT(*) 
      FROM (SELECT VALUE 
        FROM TABLENAME T1 
       WHERE T1.ID = T.ID 
       GROUP BY VALUE)) = 3; 

,其中3號的值可以通過

 SELECT COUNT(*) 
      FROM TABLENAME T1 
     GROUP BY VALUE 

所以這將是通用:

SELECT ID 
    FROM TABLENAME T 
WHERE (SELECT COUNT(*) 
      FROM (SELECT VALUE 
        FROM TABLENAME T1 
       WHERE T1.ID = T.ID 
       GROUP BY VALUE)) = (SELECT COUNT(*) 
             FROM TABLENAME T2 
            GROUP BY VALUE) 
+0

有效,但過於複雜 - 答案可以在單個「SELECT」語句中完成。此外,您的別名需要更正 – 2012-07-27 03:51:00

+0

仍然過於複雜 - 您不需要子查詢。提示:'HAVING' – 2012-07-27 03:53:48

+0

我試過這個..但是我得到所有行,即使它沒有所有的值1,2和3 – user1556549 2012-07-27 03:55:46

1

下面是一個選項... HAVING子句中每個表達式計數被發現等於1,2或3,如果任何值的數量這些計數小於1,那麼ID將不會被返回。

http://sqlfiddle.com/#!4/00fdc/8

SELECT ID 
FROM myTable 
GROUP BY ID 
HAVING 
    SUM(DECODE(VALUE, 1, 1, 0)) > 0 AND 
    SUM(DECODE(VALUE, 2, 1, 0)) > 0 AND 
    SUM(DECODE(VALUE, 3, 1, 0)) > 0 

EDIT - 規定值1,2或3個:

SELECT ID 
FROM myTable 
GROUP BY ID 
HAVING 
    SUM(DECODE(VALUE, 1, 1, 0)) > 0 AND 
    (
     SUM(DECODE(VALUE, 2, 1, 0)) > 0 OR 
     SUM(DECODE(VALUE, 3, 1, 0)) > 0 
) 
+0

方式過於複雜 – 2012-07-27 03:56:16

+2

那麼爲什麼不向我們展示它是如何完成的? – 2012-07-27 03:57:54

+0

查看我的提示hmmftg – 2012-07-27 03:58:26

0
從(選擇ID,和(情況下,當值= 1,則1

選擇ID作爲'v1',
總和(當值= 2然後1其他0結束的情況)作爲'v2',
總和(當值= 3然後1其他0結束的情況)作爲'v3'
由ID ORAC組)作爲最終
其中,V1> 0且V2> 0和v3> 0

0

有了這個選項,你會得到更多的比的ID,到你的應用程序選擇您想要的列:

SELECT ID, 
    sum(CASE WHEN VALUE = 1 THEN 1 ELSE 0 END) AS ONE, 
    sum(CASE WHEN VALUE = 2 THEN 1 ELSE 0 END) AS TWO, 
    sum(CASE WHEN VALUE = 3 THEN 1 ELSE 0 END) AS THREE 
FROM MYTABLE 
    GROUP BY ID 
    HAVING ONE >= 1 AND TWO >= 1 AND THREE >= 1; 

或者,如果你的情況是特定的(只值1,2,3是可能的,而且沒有重複值是允許的),那麼你可以嘗試以下操作之一:

SELECT ID, 
    count(VALUE) AS VALUECOUNT 
FROM MYTABLE 
    GROUP BY ID 
    HAVING VALUECOUNT = 3; 

我會以ca重新開始之前,如果您稍後想要添加其他值,則可能會產生副作用。但是,如果您目前的案例符合上述限制條件,仍然值得提出建議。

,當然,如果你不喜歡這些取中間數的想法,我圍內的另一選擇

SELECT ID FROM (
    ... 
) 
4

最簡單的方式給了查詢一般是這樣的

SQL> ed 
Wrote file afiedt.buf 

    1 with x as (
    2 select 1 id, 1 val from dual union all 
    3 select 1 id, 2 val from dual union all 
    4 select 1 id, 3 val from dual union all 
    5 select 2 id, 1 val from dual union all 
    6 select 2 id, 2 val from dual union all 
    7 select 3 id, 1 val from dual union all 
    8 select 3 id, 2 val from dual union all 
    9 select 3 id, 3 val from dual union all 
10 select 4 id, 1 val from dual 
11 ) 
12 select id 
13 from x 
14 where val in (1,2,3) 
15 group by id 
16* having count(distinct val) = 3 
SQL>/

     ID 
---------- 
     1 
     3 

WHERE子句標識您感興趣的值。HAVING子句告訴您需要存在多少這些值。例如,如果您想要所有具有3個值中的至少2個的行,則會更改HAVING子句以查找COUNT爲2。

如果一個特定的val保證每id最多發生一次,則可以消除HAVING子句中的distinct

+0

非常好的建議 – 2012-07-27 05:08:14