添加一年如果第二行月小于第一行月份

时间:2015-05-07 10:58:46

标签: sql sql-server sql-server-2008

我有以下查询

declare @tempMonth table(ID int IDENTITY(1,1) PRIMARY KEY,Monthid int)
declare @tempDay table(ID int IDENTITY(1,1) PRIMARY KEY,Day int)

declare @AddedDate table(ID int IDENTITY(1,1) PRIMARY KEY,Datee date)

declare @StartEndDate table(ID int IDENTITY(1,1) PRIMARY KEY,StartDate date,Enddate date)


insert into @tempMonth select words Months  from dbo.SplitString1('5|8|1|3','|')
insert into @tempDay select words Days  from dbo.SplitString1('10|11|12|13','|')

insert into @AddedDate 
select convert(varchar,(convert(varchar,Year)+'-'+convert(varchar,Month)+'-'+convert(varchar,Day))) Datee  from(
select td.ID,td.Day,tm.Monthid Month,  YEAR( getdate()) Year       --convert(date,convert(varchar,((Year(getdate()))+'-'+tm.Monthid+'-'+td.Day)))Datee
from @tempDay td
join @tempMonth tm on tm.ID=td.ID
) x


declare @t1 table(ID int IDENTITY(1,1) PRIMARY KEY,StartDate date)
declare @t2 table(ID int IDENTITY(0,1) PRIMARY KEY,StartDate date)


insert into @t1 select Datee StartDate from @AddedDate
insert into @t2 select Datee StartDate from @AddedDate

select t1.ID,t1.StartDate ,
case when t1.ID=4 then DATEADD(day,-1, (SELECT top 1 t3.StartDate FROM @t1 t3  ORDER BY t3.ID)) else 
DATEADD(day,-1,t2.StartDate) end EndDate from @t1 t1
left join @t2 t2 on t2.ID=t1.ID

这将产生下表

ID  StartDate   EndDate
1   2015-05-10  2015-08-10
2   2015-08-11  2015-01-11
3   2015-01-12  2015-03-12
4   2015-03-13  2015-05-09

我想增加年份如果第二列值高于 第一列值。

我尝试更改上面的最后一个选择查询

---
----
insert into @t1 select Datee StartDate from @AddedDate
insert into @t2 select Datee StartDate from @AddedDate

declare @Flag int=0;

select t1.ID,
case when @Flag=0 then t1.StartDate else DATEADD(year,1,t1.StartDate) end StartDate,

case when (month(t1.StartDate)>month(t2.StartDate)) then @Flag=1 end,

case when @Flag=0 then t2.StartDate else(DATEADD(YEAR,1,t2.StartDate)) end EndDate
from @t1 t1
left join @t2 t2 on t2.ID=t1.ID

但是发生错误,但我需要以下输出

ID  StartDate   EndDate
1   2015-05-10  2015-08-10
2   2015-08-11  2016-01-11
3   2016-01-12  2016-03-12
4   2016-03-13  2016-05-09

SplitString1函数定义是

CREATE FUNCTION [dbo].[SplitString1]( @StringValue varchar( 2000  
                                                     ) ,   
                                 @Delimiter char( 1  
                                                )  
                               )  
RETURNS @resulttable TABLE( words varchar( 2000  
                                         )  
                          )  
AS  
BEGIN  
    DECLARE  
       @index int;  
    DECLARE  
       @sliceOfStringValue varchar( 2000  
                                  );  

    SET @index = 1;  
    IF LEN( @StringValue  
          )  
       <   
       1  
    OR @StringValue IS NULL  
        BEGIN  
            RETURN  
        END;  

    WHILE @index != 0  
        BEGIN  
            SET @index = CHARINDEX( @Delimiter , @StringValue  
                                  );  
            IF @index != 0  
                BEGIN  
                    SET @sliceOfStringValue = LEFT( @StringValue , @index - 1  
                                                  );  
                END  
            ELSE  
                BEGIN  
                    SET @sliceOfStringValue = @StringValue;  
                END;  

            IF LEN( @sliceOfStringValue  
                  )  
               >   
               0  
                BEGIN  
                    INSERT INTO @resulttable( words  
                                            )  
                    VALUES( @sliceOfStringValue  
                          )  
                END;  

            SET @StringValue = RIGHT( @StringValue , LEN( @StringValue  
                                                        ) - @index  
                                    );  
            IF LEN( @StringValue  
                  )  
               =   
               0  
                BEGIN BREAK  
                END;  
        END;  
    RETURN;  
END;   

1 个答案:

答案 0 :(得分:0)

如何构建查询。如果结束日期开始日期之前,则以下内容会添加一年:

with t as (
        select t1.ID, t1.StartDate ,
               (case when t1.ID=4 then DATEADD(day,-1, (SELECT top 1 t3.StartDate FROM @t1 t3  ORDER BY t3.ID))
                     else DATEADD(day,-1,t2.StartDate)
                end) as EndDate
        from @t1 t1 left join
             @t2 t2
             on t2.ID = t1.ID
       )
select id, StartDate,
       (case when EndDate < StartDate then dateadd(year, 1, EndDate)
             else EndDate
        end) as EndDate
from t;

如果你只想关注这个月:

select id, StartDate,
       (case when month(EndDate) < month(StartDate)
             then dateadd(year, 1, EndDate)
             else EndDate
        end) as EndDate
from t;