2

嗨,有一个temp #Dates table使用以下条件填充的。

IF @Period = 'Monthly'
    BEGIN
        INSERT #Dates
        SELECT MAX(DATE) WeekDays FROM TABLENAV  WITH (NOLOCK, READUNCOMMITTED) 
        GROUP BY DATEDIFF(MONTH, 0, DATE)
        ORDER BY MAX(DATE) DESC
    END

现在数据在#Dates 临时表中

DATE
2013-04-22 00:00:00.000
2013-03-29 00:00:00.000
2013-02-28 00:00:00.000
2013-01-31 00:00:00.000
2012-12-31 00:00:00.000
2012-11-30 00:00:00.000

我想得到Previous_Date当我通过任何Todays_Date

我试过如下

DECLARE @Todays_Date datetime = '2013-04-22'
    select date from #Dates where MONTH(DATE) = MONTH(@Todays_Date)-1  AND YEAR(DATE) = YEAR(@Todays_Date)

giving ans : 2013-03-29

但是当年份改变时,我什么也得不到

例如,如果@Todays_Date = '2012-01-31'它应该返回我2012-12-31但什么都不给。

帮我修改我的select statement.

4

2 回答 2

2

You can use the below query to get the previous date,

SELECT top 1 Date FROM #Date WHERE Date <@Todays_Date order by date desc
于 2013-04-24T08:41:57.230 回答
2
select max([Date]) FROM #Date WHERE [Date] <@Todays_Date
于 2013-04-24T08:46:23.540 回答