我有一个报告,其中显示了基于时间段的结果(例如:1个月,2个月...)。我尝试查询并像这样出来。我想从当前日期开始1个月的记录
SELECT printed_serial,[name],last_active_date
FROM #Temp t
INNER JOIN [dbfastsconfigdataref].[dbo].[cdf_micard] mi on mi.cu_id = t.cu_id
INNER JOIN [dbfastsconfigdataref].[dbo].[cdf_ch_cuid] chcu on chcu.cu_id = t.cu_id
INNER JOIN [dbfastsconfigdataref].[dbo].[cdf_ch] ch on ch.ch_id = chcu.ch_id
WHERE last_active_date <= DATEADD(month, -1, getdate())
结果:
card id name last_active_date
0110 Sara jan 4 2019 4:15 pm
0111 Ara Nov 26 2013 12:22 am
但预期结果:
card id name last_active_date
0110 Sara jan 4 2019 4:15 pm
答案 0 :(得分:1)
您可以尝试以下操作-使用一个月前的日期与当前日期之间的日期比较
SELECT printed_serial,[name],last_active_date
FROM #Temp t
INNER JOIN [dbfastsconfigdataref].[dbo].[cdf_micard] mi on mi.cu_id = t.cu_id
INNER JOIN [dbfastsconfigdataref].[dbo].[cdf_ch_cuid] chcu on chcu.cu_id = t.cu_id
INNER JOIN [dbfastsconfigdataref].[dbo].[cdf_ch] ch on ch.ch_id = chcu.ch_id
WHERE last_active_date >= DATEADD(month, -1, getdate())
and last_active_date <=getdate()
答案 1 :(得分:0)
您实际上只对月份和年份感兴趣,因此可以使用以下内容:
SELECT printed_serial,[name],last_active_date
FROM #Temp t
INNER JOIN [dbfastsconfigdataref].[dbo].[cdf_micard] mi on mi.cu_id = t.cu_id
INNER JOIN [dbfastsconfigdataref].[dbo].[cdf_ch_cuid] chcu on chcu.cu_id = t.cu_id
INNER JOIN [dbfastsconfigdataref].[dbo].[cdf_ch] ch on ch.ch_id = chcu.ch_id
WHERE MONTH(last_active_date) = MONTH(DATEADD(month, -1, getdate()))
AND YEAR(last_active_date) = YEAR(DATEADD(month, -1, getdate()))