我有一个带有两列custid和flag的表Cust。并且有带有标志的记录u,i用于相同的custid。 我只想获取只有U而不是I的记录。 例如:
客户表:
custid flag
123 U
123 I
124 U
124 I
125 U
126 U
126 I
127 U
127 U
我想选择custid 125和127,因为他们没有旗帜我。
请提出建议。
答案 0 :(得分:2)
如果您将custid
分组,则只能选择不存在flag = 'I'
的那些。
select custid
from cust
group by custid
having count(case when flag = 'I'
then 1
else 0
end) = 0
答案 1 :(得分:0)
SELECT * FROM CUST
WHERE FLAG = 'U' AND
CUSTID NOT IN (SELECT CUSTID FROM CUST WHERE FLAG = 'I')
答案 2 :(得分:0)
这是另一种方法。 (的 Fiddle Example 强>)
select x.custId
from cust x
left join (select custId from cust where flag = 'I') y
on x.custId = y.custId
where y.custId is null
group by x.custId
答案 3 :(得分:0)
select distinct custid from cust
MINUS
select distinct custid from cust where flag = 'I'
答案 4 :(得分:0)
select distinct c.custid from cust c where (select count(*)
from cust where custid = c.custid and flag = 'I') = 0
获取flag
为'I'
的客户的号码,如果是equal to 0
,则选择custid
。
答案 5 :(得分:0)
select a.num, a.custid
from
(select count(flag) as num, custid
from cust
group by custid) as a
inner join
(select count(flag) as num, custid
from cust
where flag = 'U'
group by custid) as b
on a.custid = b.custid
and a.num = b.num
这是结果: