Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Selecting last three records
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2008
Miscellaneous
Thread ID:
01539026
Message ID:
01539030
Views:
36
Nick,

Sergey's response will work for any 3 payments (not the necessarily the last 3) not made. This would handle only looking at the last 3 scheduled payments -
SELECT memberid FROM 
(SELECT memberid, duedate, paid, ROW_NUMBER() OVER (PARTITION by memberid ORDER by duedate DESC) AS rownum FROM payments 
WHERE duedate < GETDATE()) a 
WHERE rownum <= 3 AND NOT (paid = 'true') GROUP BY memberid HAVING COUNT(*) = 3
You will need to adjust the NOT (paid = 'true') condition if you allow nulls in the paid column.

-Dan

>Hi
>
>I have a membership database. With a members table and a payments table
>
>Members can now pay by monthly instalments.
>If they've elected to do that then the payments table has 12 due payments set up for the member and as they pay those payments are marked as paid.
>
>I want to suspend any member who has not paid their last 3 monthly instalments.
>
>So I'm going to have a regular scheduled job that goes through and suspends members member.
>
>What query do I want to find members whose last 3 payments are still marked as due.
>
>Thanks
>
>Nick
Previous
Reply
Map
View

Click here to load this message in the networking platform