2016-04-27 99 views
0

如何将1个月添加到日期值(第1行)并将其用作下一行的输入以添加月份直到达到最大月份ID。将1个月添加到日期值

declare @tmp table (date date,month_id int); 
insert into @tmp values('2014-11-30',1),('2014-11-30',2),('2014-11-30',3),('2014-11-30',4),('2014-11-30',5),('2014-11-30',6),('2015-01-01',1),('2015-01-01',2),('2015-01-01',3),('2015-01-01',4); 

Result Set

预期输出:

DATE  month_id derived_date1 
2014-11-30  1  2014-12-30 
2014-11-30  2  2015-01-30 
2014-11-30  3  2015-02-28 
2014-11-30  4  2015-03-28 
2014-11-30  5  2015-04-28 
2014-11-30  6  2015-05-28 
2015-01-01  1  2015-02-01 
2015-01-01  2  2015-03-01 
2015-01-01  3  2015-04-01 
2015-01-01  4  2015-05-01 
+0

请显示所需的结果集。您的描述不清楚。另外我猜你正在使用'SQL Server'。 – lad2025

+1

在标准SQL中,您可以执行'date_column + interval'1'month'。你正在使用哪个DBMS? –

+0

预期输出: DATE \t \t month_id derived_date1 2014年11月30日\t \t 1二○一四年十二月三十日 2014年11月30日\t \t 2 2015年1月30日2014年11月30日 \t 2015-02 -28 2014年11月30日\t \t 4 2015年3月28日2014年11月30日 \t 2015年4月28日2014年11月30日 \t 2015-05-28 2015年1月1日 2015-02-01 2015-01-01 2015年3月1日2015年1月1日 \t 2015-04-01 2015年1月1日\t \t 4 2015年5月1日 –

回答

1

递归CTE

declare @tmp table (date date,month_id int); 
insert into @tmp values('2014-11-30',1),('2014-11-30',2),('2014-11-30',3),('2014-11-30',4),('2014-11-30',5),('2014-11-30',6),('2015-01-01',1),('2015-01-01',2),('2015-01-01',3),('2015-01-01',4); 

;with cte as 
(
    select date, month_id, DATEADD(MONTH, 1, date) as derived_date1 from @tmp where month_id = 1 
    union all select t.date, t.month_id, DATEADD(MONTH, 1, cte.derived_date1) from cte inner join @tmp t on cte.date = t.date and cte.month_id = t.month_id - 1 
) 
select * from cte order by date, month_id 
+0

谢谢埃里克它的工作原理.. .i试了一下,错过了'-1' –

0

你可以试试这个

DATEADD()功能添加或减少日期指定的时间间隔。

Declare @tmp table (date date,month_id int); 
    Insert into @tmp values('2014-11-30',1),('2014-11-30',2),('2014-11-30',3),('2014-11-30',4),('2014-11-30',5),('2014-11-30',6),('2015-01-01',1),('2015-01-01',2),('2015-01-01',3),('2015-01-01',4); 
    SELECT date as Original_Date,month_id as Month_Id,DATEADD(MONTH,month_id,date) as Derived_date from @tmp 

Result Set

+0

我想要的第一行输出作为输入到第二行等等...上述查询我试图较早 –

0

您可以通过使用数字或理货表

declare @numbers table(no int) 
insert into @numbers 
select 1 union all 
select 2 union all 
select 3 union all 
select 4 union all 
select 5 union all 
select 6 union all 
select 7 union all 
select 8 union all 
select 9 union all 
select 10 union all 
select 11 union all 
select 12 

declare @month_value int, @date datetime 
select @month_value=12, @date='2014-01-30' 

declare @tmp table (date date,month_id int); 
insert into @tmp 
select dateadd(month,no-1,@date),no from @numbers where no<[email protected]_value 

select * from @tmp 
+0

预期输出: DATE \t \t month_id derived_date1 2014年11月30日\t \t 1二〇一四年十二月三十零日 2014年11月30日\t \t 2 2015年1月30日2014年11月30日 \t \t 3 2015年2月28日2014年11月30日 \t 2015年3月28日2014年11月30日 \t 2015年4月28日2014年11月30日 \t 2015-05-28 2015-01-01 2015-02-01 2015年1月1日 2015年3月1日 2015年1月1日 2015-04-01 2015年1月1日 2015年5月1日 –

0

对于一个给定的日期,你可以使用一个月范围内做到这一点CTE和关于数据的加入:

declare @StartMonth int = 1 
declare @EndMonth int = 6 

declare @DateToPopulate Date = '2014-11-30' 
declare @tmp table (original date,month_id int, derived date) 
; 

with month_range as 
(
    select @StartMonth as Month 
    union all 
    select Month+1 from month_range where Month+1 <[email protected] 
), 
generated_data as 
(
    select @DateToPopulate as OriginalDate, Month as MonthID, DATEADD(month,Month,@DateToPopulate) as DerivedDate 
    from month_range 
) 
insert into @tmp 
select * from generated_data