0

我有一个表 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。

请提出查询。

4

6 回答 6

2

如果您围绕您进行分组,custid则只能选择不flag = 'I'存在的那些。

select custid
from cust
group by custid
having count(case when flag = 'I' 
                  then 1 
                  else 0
             end) = 0
于 2013-09-02T14:54:12.107 回答
0
select distinct custid from cust
MINUS
select distinct custid from cust where flag = 'I'
于 2013-09-02T15:11:12.683 回答
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

这是结果:

在此处输入图像描述

于 2013-09-02T16:44:45.223 回答
0
SELECT * FROM CUST
WHERE FLAG = 'U' AND
  CUSTID NOT IN (SELECT CUSTID FROM CUST WHERE FLAG = 'I')
于 2013-09-02T15:03:43.930 回答
0
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它。

于 2013-09-02T15:16:06.393 回答
0

这是另一种方法。(小提琴示例

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
于 2013-09-02T15:07:31.727 回答