我有一个表 Cust 有两列 custid 和 flag.And 有带有标志 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,因为它们没有标志 I。
请提出查询。
我有一个表 Cust 有两列 custid 和 flag.And 有带有标志 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,因为它们没有标志 I。
请提出查询。
如果您围绕您进行分组,custid则只能选择不flag = 'I'存在的那些。
select custid
from cust
group by custid
having count(case when flag = 'I'
then 1
else 0
end) = 0
select distinct custid from cust
MINUS
select distinct custid from cust where flag = 'I'
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
这是结果:

SELECT * FROM CUST
WHERE FLAG = 'U' AND
CUSTID NOT IN (SELECT CUSTID FROM CUST WHERE FLAG = 'I')
select distinct c.custid from cust c where (select count(*)
from cust where custid = c.custid and flag = 'I') = 0
获取具有flagas的客户编号'I',如果是equal to 0则选择custid它。
这是另一种方法。(小提琴示例)
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