1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| declare @mytable TABLE (
code INTEGER NOT NULL
,date DATE NOT NULL
,value INTEGER NOT NULL
);
INSERT INTO @mytable(code,date,value) VALUES (1000,'2016-08-05',5000);
INSERT INTO @mytable(code,date,value) VALUES (1000,'2016-12-27',8000);
INSERT INTO @mytable(code,date,value) VALUES (1000,'2018-03-19',6000);
INSERT INTO @mytable(code,date,value) VALUES (1000,'2018-06-02',6000);
with nums(i) as (
select 0
union all select i + 1 from nums where i < 24
)
select
t.code,t.date,
dateadd(month, i, t.date) date,
t.value
from @mytable t
inner join nums n
on not exists (
select 1
from @mytable t1
where t1.date > t.date and eomonth(t1.date) < eomonth(dateadd(month, i + 1, t.date))
)
order by t.date |
Partager