Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
oracle11g中的SQL语句_Sql_Oracle_Oracle11g - Fatal编程技术网

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 

您的要求是查找ID与
代码
状态
的五种组合相匹配的记录。满足此要求的一个简单方法是将每个组合指定为单独的子查询,并使用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
。请再次解释条件,并提供输出示例?