Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一张表,我想获得 s# 的表,其中 s# 有 p1 但没有 p2;
表 s s# P#
s1 p1
s1 p2
s1 p3
s1 p4
s2 p1
s2 p3
s3 p2
s3 p3
我的结果应该是 s2。我尝试的 where 子句和连接返回 s1,s2 这是错误的。
尝试这个:
SELECT s# FROM (SELECT s#, SUM(CASE WHEN p# = 'p1' THEN 1 ELSE 0 END) p1ct , SUM(CASE WHEN p# = 'p2' THEN 1 ELSE 0 END) p2ct FROM s GROUP BY s#) WHERE p1ct > 0 AND p2ct = 0;