question

kostaras avatar image
kostaras asked

Select Last PaymentDay

I have 2 tables: FightersInfo and PaymentInfo. 1 fighter got many payment days. I need help to select last payment day. > SELECT FName, LName, PaymentDay, PaymentDescr,PaymentAmount, Active WHERE (PaymentAMount= 0 OR PaymentAmount = NULL OR PaymentDay = NULL OR "Last PaymentDay is before 1 month ago") AND Active =1 i want to get only the Last payment of fighter if is before a month ... For example if a fighter has many payments only before a month i want the last payment But if a fighter has a lot payments before a month and gots a payment in last month i dont want him in my select here is schema of my tables http://sqlfiddle.com/#!6/3d861/3 i want to return only the last PaymentDay of FighterID=1 Causes the FighterID=2 he paid in last month
sql-server-2008sql-server-2012
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
David Wimbush avatar image
David Wimbush answered
You could use `row_number() over (partition by FighterID order by PaymentDay desc)` to get the details of the each fighter with their latest PaymentDay. Then compare that date with `dateadd(month, -1, getdate())`.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.