某个表T
Name IsHidden
====================
A 0
B 0
B 1
C 1
D 1
D 0
欲求如下结果集:每个Name取一个值,如果有记录IsHidden=0(未隐藏),则取该行。如果没有IsHidden=0的,只有IsHidden=1的,如上面的C,则取该行。使得求出的结果如下:
Name IsHidden
====================
A 0
B 0
C 1
D 0
有没有办法直接用SQL求出呢?
如下:
select Name, IsHidden from T as tt
where IsHidden=0 or
((select count(1) from T where tt.Name = Name and IsHidden=0)=0
and IsHidden=1)
select [Name],case min([IsHidden]) when 0 then 0 else 1 end from t
group by [name]