首页 > 解决方案 > 创建所需的脚本

问题描述

我的问题是关于选择语句。我有一个名为test1的表和其中的值。这是我用于创建表和插入值的脚本:

create table test1(id number, pc number, pe number);
insert into test1 values(12,0,900); 
insert into test1 values(12,0,901);
insert into test1 values(12,0,902);
insert into test1 values(12,91,900);
insert into test1 values(12,0,1);
insert into test1 values(12,91,900); 
insert into test1 values(12,91,901); 
insert into test1 values(12,91,900);
insert into test1 values(12,91,5);
insert into test1 values(13,0,900);
insert into test1 values(12,0,20); 
insert into test1 values(12,1,1);
insert into test1 values(12,0,900);
insert into test1 values(13,91,900); 
insert into test1 values(13,91,901); 
insert into test1 values(13,91,902); 
insert into test1 values(13,0,902);
insert into test1 values(13,91,201); 
insert into test1 values(13,91,202);
insert into test1 values(13,91,20);
insert into test1 values(13,0,900);
insert into test1 values(13,0,900); 
commit;

我的问题是如何从test1表中选择pc列仅包含0 或 91pe列仅包含20、201、202、900、901 或 902的Id。所以ID就像13

标签: sqloraclesql-scripts

解决方案


只需检查满足行数是否等于 count(*):

select id
from test1
group by id
having 
   count(*) = count(case when pc in (0,91) and pe in (20, 201, 202, 900, 901, 902) then 1 end)

推荐阅读