oracle选择具有相同columng但不同于其他值的值

时间:2018-05-08 12:19:05

标签: sql oracle

我在表中有这个值:

table values

我需要选择相同的ID,其中VALUE都是W: ID ; 示例ID:534103

3 个答案:

答案 0 :(得分:0)

使用id

not exists

但是,您也可以使用select distinct t.id from table t where not exists (select 1 from table where id = t.id and value != 'W');

GROUP BY

修改

select id
from table t
group by id
having sum(case when value != 'W' then 1 else 0 end) = 0;

答案 1 :(得分:0)

您可以使用聚合:

select id
from t
group by id
having min(value) = max(value) and min(value) = 'W';

答案 2 :(得分:0)

您可以使用HAVING子句:

SELECT ID
FROM   your_table
GROUP BY ID
HAVING COUNT( CASE value WHEN 'W' THEN 1 END ) = COUNT(*)

或:

SELECT ID
FROM   your_table
GROUP BY ID
HAVING COUNT( CASE WHEN value = 'W' THEN NULL ELSE 1 END ) = 0

或者,如果您想要匹配的组的所有行:

SELECT *
FROM   (
  SELECT t.*,
         COUNT( CASE WHEN value < > 'W' THEN 1 END ) OVER ( PARTITION BY id )
           AS Num_Non_W
  FROM   your_table
)
WHERE  Num_Non_W = 0