一个有趣的棘手的过滤器(不知道如何在标题中描述它)

Wil*_* Xu 1 sql t-sql sql-server

我的桌子是这样的:

Item   Subitem    Progress  
1      101        Complete  
1      102        Pending
1      103        Pending
2      201        Complete
2      202        Complete
Run Code Online (Sandbox Code Playgroud)

我想选择子项全部完整的项目 - 在这种情况下,项目#2.知道怎么做吗?谢谢!

sha*_*t00 6

这么多方面:

select Item from T group by Item
having min(Progress) = max(Progress) and max(Progress) = 'Complete'
Run Code Online (Sandbox Code Playgroud)

要么

having count(case when Progress = 'Complete' then 1 end) = count(*)
Run Code Online (Sandbox Code Playgroud)

要么

having count(case when Progress = 'Complete' then null else 1 end) = 0 /* handles nulls */
Run Code Online (Sandbox Code Playgroud)

要么

select distinct Item from T t
where 'Complete' = all (select Progress from T t2 where t2.Item = t.Item)
Run Code Online (Sandbox Code Playgroud)