我如何 select 那些对每个条目只有零贡献的人的所有行
How do I select all rows for those persons who have only zero contributions for every entry
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 年的贡献。另请注意
结果列表显示每年有一个人
会费为零——因为它不对
结果个体,这很重要。
免责声明:IDK 如果在这里使用 CTE 是矫枉过正。但它有效
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
我认为您需要的是这样的查询
SELECT
*
FROM
Contribution_Table
WHERE CONTRIBUTION=0
AND ID NOT IN
(SELECT ID FROM Contribution_Table WHERE CONTRIBUTION > 0)
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 年的贡献。另请注意 结果列表显示每年有一个人 会费为零——因为它不对 结果个体,这很重要。
免责声明:IDK 如果在这里使用 CTE 是矫枉过正。但它有效
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
我认为您需要的是这样的查询
SELECT
*
FROM
Contribution_Table
WHERE CONTRIBUTION=0
AND ID NOT IN
(SELECT ID FROM Contribution_Table WHERE CONTRIBUTION > 0)