【发布时间】:2010-06-17 00:08:38
【问题描述】:
我使用的是 SQL-SERVER 2005。
如图所示,我有两张表,CellularUsers 和 CellularPayments。我需要在规则很少的情况下获取需要收费的用户:
- 如果最后一个 CellularPayments.paymentSum of userID 为 8,则选择所有 userID 和 userCellularNumbers 其中 CellularPayments.date>getdate()
- 如果最后一个 CellularPayments.paymentSum of userID 为 18,则选择所有 userID 和 userCellularNumbers 其中 dateadd(dd,7,CellularPayments.date)>getdate()
alt text http://img256.imageshack.us/img256/1946/63468037.png
预览图click here
我现在是怎么做的,但我觉得不合适
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)
提前致谢
【问题讨论】:
标签: sql sql-server tsql