mardi 6 janvier 2015

SQL statement monthly payment check

I have a project about gym management and I want to check monthly payments.


For example, I want to check the last time a member paid, if the payment day was 30 days before, and if his status is active.


Here is my code, but it needs to be fixed.



SELECT * FROM (
SELECT
pi.FighterID,
FName,
LName,
Convert(varchar(20),
PaymentDay,1),
PaymentDescr,
PaymentAmount,
Active,
ROW_NUMBER() OVER (PARTITION BY pi.FighterID ORDER BY PaymentDay DESC) rn
FROM
FightersInfo fi LEFT JOIN
PaymentInfo pi ON pi.FighterID = fi.FighterID
WHERE
NOT EXISTS (SELECT * FROM PaymentInfo WHERE FighterID = fi.FighterID
AND DATEDIFF(day, PaymentDay, GETDATE()) <= 30 )
AND Active =1) t
WHERE rn = 1


This only returns the first member. If I remove WHERE rn =1, it returns all payments. I only want the last payment of each member.


Aucun commentaire:

Enregistrer un commentaire