oracle11g中的SQL语句
我有以下格式的表格:oracle11g中的SQL语句,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我有以下格式的表格: ID code status ----- ----- ------ 1 Dept1 200 1 Dept2 500 1 Dept3 500 1 Dept3 200 2 Dept1 200 2 Dept2 500 2 Dept3 500 2 Dept3 500 3 Dept1 200
ID code status
----- ----- ------
1 Dept1 200
1 Dept2 500
1 Dept3 500
1 Dept3 200
2 Dept1 200
2 Dept2 500
2 Dept3 500
2 Dept3 500
3 Dept1 200
3 Dept2 500
3 Dept3 500
3 Dept2 200
4 Dept1 500
4 Dept2 500
4 Dept3 500
我需要的输出是ID 1和ID 3
有谁能帮我写一个SQL来打印ID 1和ID 3,因为它们的状态是Dept 2和Dept 3都是200和500,Dept 1都是200您需要
条件聚合
根据您的解释,只有ID=3
满足所有条件
SELECT id
FROM Yourtable
GROUP BY id
HAVING Count(CASE WHEN "code" = 'Dept2' AND "status" = 200 THEN 1 END) = 1
AND Count(CASE WHEN "code" = 'Dept3' AND "status" = 500 THEN 1 END) = 1
AND Count(CASE WHEN "code" = 'Dept1' AND "status" = 200 THEN 1 END) = 1
代码
和状态
的五种组合相匹配的记录。满足此要求的一个简单方法是将每个组合指定为单独的子查询,并使用INTERSECT set运算符将结果减少为唯一ID:
select id from your_table
where code = 'Dept1' and status = 200
intersect
select id from your_table
where code = 'Dept2' and status = 200
intersect
select id from your_table
where code = 'Dept2' and status = 500
intersect
select id from your_table
where code = 'Dept3' and status = 200
intersect
select id from your_table
where code = 'Dept3' and status = 500
1
没有dept2和dept3
两个200
和500
。请再次解释条件,并提供输出示例?