我有一个select查询,结果如下:
ID COMPLIANT
------------------
10 0
12 0
29 0
29 1
43 1
44 1
44 0如果一个ID已经被标记为合规一次(1而不是0),那么我如何在没有这些重复ID行的情况下获得结果,带有COMPLIANT=0的重复行不会出现?我想:
ID COMPLIANT
------------------
10 0
12 0
29 1
43 1
44 1发布于 2018-10-12 04:10:52
那么聚合呢?
select id, max(complaint) as complaint
from t
group by id;这将为每个id返回一行。如果你可以有多个抱怨--你想要所有的抱怨--那么另一个选择是:
select id, complaint
from t
where complaint = 1
union all
select id, complaint
from t
where not exists (select 1 from t t2 where t2.id = t.id and t2.complaint = 1);发布于 2018-10-12 05:15:54
这将会起作用:
select id, max(complaint)
from tablename
group by id;https://stackoverflow.com/questions/52768174
复制相似问题