Here is a example of the table I want to query: Contribution_Table ID LASTNAME FIRSTNAME CONTRIBUTION YEAR 392 Bob Sully 0.00 2012 392 Bob Sully 0.00 2013 392 Bob Sully 0.00 2014 465 John Greene 0.00 2012 465 John Greene 5.60 2013 465 John Greene 0.00 2014 892 Jane Crain 0.00 2012 892 Jane Crain 0.00 2013 892 Jane Crain 0.00 2014 I want as a result only a list containing those individuals who have never made a contribution, like so: ID LASTNAME FIRSTNAME CONTRIBUTION YEAR 392 Bob Sully 0.00 2012 392 Bob Sully 0.00 2013 392 Bob Sully 0.00 2014 892 Jane Crain 0.00 2012 892 Jane Crain 0.00 2013 892 Jane Crain 0.00 2014
请注意,John Greene被排除在外,因为他做了 2013年的贡献,即使他没有 2012年和2014年的捐款。另请注意 结果列表显示每个人一个人 没有会费 - 因为它没有总和或分组 由此产生的个人,这很重要。
答案 0 :(得分:1)
免责声明:如果在这里使用CTE,则IDK过大。但它有效
with cte as
(
select ID, SUM(CONTRIBUTION) as csum from tab group by ID
)
select * from tab where tab.id in (select cte.id from cte where cte.csum = 0)
这会奏效。继承人工作fiddle
答案 1 :(得分:1)
我认为你需要的是一个查询
SELECT
*
FROM
Contribution_Table
WHERE CONTRIBUTION=0
AND ID NOT IN
(SELECT ID FROM Contribution_Table WHERE CONTRIBUTION > 0)