会员表每年每个成员包含1条记录(本例简化。
我想要那些没有续约2014年的2013年会员。这就像我逻辑上来的那样接近但我的结果是空的
SELECT Users.UserID
, YEAR(Membership.MemYear) AS MemYear
, Users.MailTo
, Users.StreetAddress
, Users.Address2
, Users.City
, StateLookup.State
, Users.Zip
FROM Users INNER JOIN Membership
ON Users.UserID = Membership.UserID
INNER JOIN StateLookup
ON Users.StateID = StateLookup.StateID
where YEAR(Membership.MemYear) = '2013'
and not exists
(SELECT Users.UserID, YEAR(Membership.MemYear) AS MemYear
, Users.MailTo, Users.StreetAddress, Users.Address2
, Users.City, StateLookup.State, Users.Zip
FROM Users INNER JOIN Membership AS Membership_1
ON Users.UserID = Membership_1.UserID
INNER JOIN StateLookup
ON Users.StateID = StateLookup.StateID
where YEAR(Membership_1.MemYear)='2014')
帮助表示赞赏!
答案 0 :(得分:1)
我认为如果'UserID'是唯一的,你可以这样做。
SELECT Users.UserID
, YEAR(Membership.MemYear) AS MemYear
, Users.MailTo
, Users.StreetAddress
, Users.Address2
, Users.City
, StateLookup.State
, Users.Zip
FROM Users INNER JOIN Membership
ON Users.UserID = Membership.UserID
INNER JOIN StateLookup
ON Users.StateID = StateLookup.StateID
WHERE Membership.MemYear = '2013'
AND Users.UserID NOT IN
(SELECT UserID FROM Membership WHERE MemYear = '2014');
答案 1 :(得分:1)
您可以使用MAX(MemYear)
查找2013年最长纪念日的人。这意味着他们在2013年有效并且尚未续订。
SELECT Users.UserID
, Users.MailTo
, Users.StreetAddress
, Users.Address2
, Users.City
, StateLookup.State
, Users.Zip
FROM Users
INNER JOIN Membership
ON Users.UserID = Membership.UserID
INNER JOIN StateLookup
ON Users.StateID = StateLookup.StateID
GROUP BY Users.UserID
, Users.MailTo
, Users.StreetAddress
, Users.Address2
, Users.City
, StateLookup.State
, Users.Zip
HAVING MAX(YEAR(Membership.MemYear)) = 2013
答案 2 :(得分:0)
尝试按如下方式更改查询的底部:
您忘记引用回查询的其他图层。假设你的查询的其余部分没问题,这应该可以解决这个问题。
SELECT Users.UserID,
YEAR(Membership.MemYear) AS MemYear,
Users.MailTo,
Users.StreetAddress,
Users.Address2,
Users.City,
StateLookup.State,
Users.Zip
FROM Users
INNER JOIN Membership
ON Users.UserID = Membership.UserID
INNER JOIN StateLookup
ON Users.StateID = StateLookup.StateID
where YEAR(Membership.MemYear) = '2013'
and not exists (SELECT x.UserID,
YEAR(Membership.MemYear) AS MemYear,
x.MailTo,
x.StreetAddress,
x.Address2,
x.City,
StateLookup.State,
x.Zip
FROM Users x
INNER JOIN Membership AS Membership_1
ON x.UserID = Membership_1.UserID
INNER JOIN StateLookup
ON x.StateID = StateLookup.StateID
where YEAR(Membership_1.MemYear) = '2014'
and x.UserID = Users.UserID)
答案 3 :(得分:0)
SELECT Users.UserID
, MemYear
, Users.MailTo
, Users.StreetAddress
, Users.Address2
, Users.City
, StateLookup.State
, Users.Zip
FROM Users INNER JOIN Membership
ON Users.UserID = Membership.UserID
INNER JOIN StateLookup
ON Users.StateID = StateLookup.StateID
where Users.UserID NOT IN
(SELECT UserID
FROM Membership
where MemYear ='2014')
答案 4 :(得分:0)
SELECT Users.UserID
, YEAR(Membership.MemYear) AS MemYear
, Users.MailTo
, Users.StreetAddress
, Users.Address2
, Users.City
, StateLookup.State
, Users.Zip
FROM Users INNER JOIN Membership ON Users.UserID = Membership.UserID
INNER JOIN StateLookup ON Users.StateID = StateLookup.StateID
where YEAR(Membership.MemYear) <= 2013 /* If it Integer you don't need '' */
为什么要使用子查询?