开发者

TSQL Help with simple query

开发者 https://www.devze.com 2023-01-03 21:37 出处:网络
I\'m using SQL-SERVER 2005. I have two tables as you can see on diagram, CellularUsers and CellularPayments. I need to getusers who needs to be charged when there are few rules:

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:

  1. if last CellularPayments.paymentSum of userID was 8 then select all userID and userCellularNumbers where CellularPayments.date>getdate()
  2. 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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消