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
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())`.