我需要选择当月只有付款日期的记录,因此历史上不会有任何其他记录,所以如果有人在这个月付款但在上个月付款,则不会返回
我很确定我会因为没有把它弄清楚而自责......
答案 0 :(得分: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)
答案 1 :(得分: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
)