我需要更新过去 14 天内未参加的会员记录。有两个表:成员和出席。如果最近 14 天的 Attend_Freq 小于 5,我需要使用最新的“Last_Attend_Date”更新会员记录。
Members table:
ID Attend_Freq Last_Attend_Date
123 4 2012-7-5 -- This is the result of the query, the most
recent date in the table instead of the
most recent for the member
Attendance
ID Member_ID Last_Attend_Date
987 123 2012-6-5
888 123 2012-6-4
567 123 2012-6-3
456 234 2012-6-30
1909 292 2012-7-5
这是查询,但它给了我出席表中最新的 Last_Attend_Date,而不是我需要更新的成员的最新日期。
UPDATE M
SET Last_Attend_Date =
(SELECT Max(Last_Attend_Date) FROM Attendance A
JOIN Members M ON A.Member_ID = M.ID
WHERE A.Member_ID =M.id )
FROM Members M
JOIN Attendance A on A.Member_ID = M.id
WHERE Attend_Freq <'5' and Last_Attend_Date <getdate()-14 and A.Member_ID = M.ID