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开发者_如何学JAVA userID and userCellularNumbers where dateadd(dd,7,CellularPayments.date)>getdate()
alt text http://img256.imageshack.us/img256/1946/63468037.png
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
If I'm following your logic correctly, adding the following AND statement to the where clause should do it:
and dateadd( dd
,case cp.PaymentSum
when 8 then 0
when 18 then 7
else [AppropriateDefaultValue]
end
,CellularPayments.date) > getdate()
(I'd also make it an inner join.)
精彩评论