用于检查列中所有值的SQL

时间:2012-07-27 03:34:04

标签: sql oracle

我在Oracle DB中有以下表格。

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

如何选择具有全部3个值(1,2,3)

的ID

5 个答案:

答案 0 :(得分:5)

最简单的选择通常是这样的

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个。

如果确保特定valid次最多发生一次,则可以删除distinct子句中的HAVING

答案 1 :(得分: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)

答案 2 :(得分: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

编辑 - 要求值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
  )

答案 3 :(得分:1)

从(选择id,sum(值= 1然后1,0结束时的情况)选择id为'v1',
sum(值= 2然后1,0结束时的情况)为'v2',
sum(值= 3然后1,0结束时的情况)为'v3'
 来自orac group by id)作为最终成员  其中v1> 0且v2> 0且v3> 0

答案 4 :(得分:1)

使用此选项,您将获得更多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;

在走这条路之前我会小心,因为如果以后你想要添加额外的值,可能会产生副作用。但是,如果您目前的案例符合上述限制,那么仍然值得提出。

当然,如果您不喜欢获取这些中间计数的想法,请附上我在另一个选择中提供的查询

SELECT ID FROM (
  ...
)