我正在寻找能够找到每年拥有相同会员资格的订单项的SQL代码。我正在尝试编写一个代码,这个代码可以让我计算所有年份非成员的记录。我需要帮助,例如
这是我的查询
select count(lname),
Year,
MembershipType
from Registrant
where Year in
('2014', '2013', '2012', '2011', '2010', '2009', '2008', '2007', '2006', '2005')
and MembershipType = 'NonMember'
order by LastName
这是我的数据集
LName....Year.... Membershiptype
Smith......2014....Non-Member
Smith......2013....Member
Smith......2012....Member
Lee.........2014....NonMember
Lee.........2013....NonMember
Bob.........2014....NonMember
Bob.........2011....NonMember
Bob..........2010....NonMember
Harry........2010....Member
Harry........2011....NonMember
Harry........2012....Member
这应该是我的结果
计数= 2
*这将是结果,因为lee和bob每年都是非成员。当我做这些计数时,它会拉起所有拥有membertype"非成员"每年。因此,例如,在我的名单列表中,初始代码会拉起哈里,史密斯,李和鲍勃,因为他们是非成员一年或多年。我想要它做的只是计算Lee和bob,因为他们每年都是非会员。请帮忙
了解详情:http://www.justanswer.com/computer-programming/8z44z-i-m-looking-code-find-counts.html#ixzz3TzerpMpA
可选信息: 语言(或软件):SQL
了解详情:http://www.justanswer.com/computer-programming/8z44z-i-m-looking-code-find-counts.html#ixzz3TzatPbvA
答案 0 :(得分:0)
听起来你需要在没有会员资格的情况下获取不同的姓氏,并删除那些曾经是会员的人(除外)。
SELECT COUNT(*)
FROM
(
select distinct
lname
from Registrant
where Year in ('2014', '2013', '2012', '2011', '2010', '2009', '2008', '2007', '2006', '2005')
and MembershipType = 'NonMember'
EXCEPT
select distinct
lname
from Registrant
where Year in ('2014', '2013', '2012', '2011', '2010', '2009', '2008', '2007', '2006', '2005')
and MembershipType <> 'NonMember'
) Inn;