创建所需的脚本

问题描述

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

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

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

解决方法

只需检查满足条件的行数等于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)
,

这是适合您的sql

select id from test1 where pc in (0,902);
,
select A,B from(
    select decode(pc,91,id,0) A,decode(pe,20,902,0) B from test1
) where A <> B;
,

从(选择解码(id,12,13,13)选择不同的c 其中a.pc = b.pc和b.pe = a.pe和a.id = b.id)其中1 = 1) 其中c = 13;