我有一张这样的桌子
create table test (custID varchar(20), currNo int)
insert into test
values ('00123', 1) ,('00123', 2), ('00123', 3),
('00124', 2), ('00124', 3),
('00125', 3),('00125', 4),
('00126', 1),('00126', 3)我只需要选择那些currNo != 1但currNo >1的custID
下面是我的代码片段;
select distinct custID from test
where currNo != 1 and currNo > 1以上查询结果:
00123
00124
00125
00126异常结果:
00124
00125请更正我的查询以获得所需的输出。提前谢谢。
发布于 2018-08-14 14:49:45
使用适用于currNo的简单GROUP BY和HAVING条件
SELECT custID FROM #test
GROUP BY custID
HAVING MIN(currNo)<>1结果
custID
00124
00125发布于 2018-08-14 14:48:29
select distinct contractid from test
where not exists(select * from test t2 where t2.contractid=test.contractid and t2.currNo=1)发布于 2018-08-14 14:51:04
使用not in
select distinct custID from test
where custID not in (select custID from test t2 where t2.custID=test.custID and t2.currNo=1)http://sqlfiddle.com/#!18/ee7c0e/10
custID
00124
00125https://stackoverflow.com/questions/51835188
复制相似问题