I'm using SQL-SERVER 2005.
I have two tables as you can see on diagram, CellularUsers and CellularPayments. I need to get users who needs to be charged when there are few rules:
- if last CellularPayments.paymentSum of userID was 8 then select all userID and userCellularNumbers where CellularPayments.date>getdate()
- if last CellularPayments.paymentSum of userID was 18 then select all userID and userCellularNumbers where dateadd(dd,7,CellularPayments.date)>getdate()
to preview diagram click here
how i do it right now but it doesn't looks right to me
select cu.userID,cu.userCellularNumber from CellularUsers cu
left join CellularPayments cp
on cu.userID=cp.userID
where cu.isPaymentRecurring=1
and isActivated=1
and cp.paymentID in (select max(paymentID) from CellularPayments group by userID)
thanks in advance