0

我需要选择当月只有付款日期的记录,这样历史上就没有其他记录了,所以如果有人本月付款但上个月已经付款,则不会返回。

很确定我会因为没有弄清楚这一点而踢自己...

4

2 回答 2

0

您可以尝试以下相关查询:

declare @ms date, @me date

set @ms = getdate()
set @ms = dateadd(day, 1-datepart(day, @ms), @ms)
set @me = dateadd(month, 1, @ms)

select t.*
from TableName t
where t.PaymentDate >= @ms and t.PaymentDate < @me
    and not exists (
        select 1 from TableName t2
        where t2.PayerID = t.PayerID and t2.PaymentDate < @ms
    )
于 2013-09-10T13:06:06.123 回答
0
declare @currentMonth int, @currentYear
select @currentMonth = MONTH(GETDATE()), @currentYear = YEAR(GETDATE())

select * from your_table 
where MONTH(paymentDate) = @currentMonth and YEAR(paymentDate) = @currentYear 
and id not in 
     (select id from your_table where MONTH(paymentDate) < @currentMonth and 
      YEAR(paymentDate) == @currentYear OR YEAR(paymentDate) != @currentYear)
于 2013-09-10T12:25:32.523 回答