Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在计算年初至今时填写缺失的月份_Sql_Sql Server - Fatal编程技术网

Sql 在计算年初至今时填写缺失的月份

Sql 在计算年初至今时填写缺失的月份,sql,sql-server,Sql,Sql Server,我今天有一张桌子 year month qty_ytd 2017 01 20 2017 02 30 2018 01 50 我需要填写gabs在同一年中缺失的月份,直到12月: 结果举例如下: year month qty_ytd 2017 01 20 2017 02 30 2017 03 30 ..... 2017 07 30 2017 12 30 2018 01 50 2018 02 50 ....

我今天有一张桌子

year month qty_ytd
2017   01    20
2017   02   30
2018   01    50
我需要填写gabs在同一年中缺失的月份,直到12月:

结果举例如下:

year month qty_ytd
2017   01    20
2017   02   30
2017   03   30
.....
2017   07   30
2017   12   30

2018   01    50
2018   02    50
....
2018   12    50
怎么做?我没有弄清楚如何填充缺少的月份?

您可以使用交叉连接生成行并交叉应用以获取数据:

select y.y, v.m, t.qty_ytd
from (select distinct year from t) y cross join
     (values (1), (2), (3), (4), . . . (12)) v(m) outer apply
     (select top (1) t.*
      from t
      where t.year = y.year and
            t.month <= y.m
      order by t.m desc
     ) t;

另一个选项是计算增量,添加虚拟零增量,恢复运行总计。我更改了源数据以显示更常见的情况

create table #t
(
 year int,
 month int, 
 qty_ytd int
);

insert #t(year, month, qty_ytd )
values
(2017,   01,   20),
(2017,   02,   30),
(2018,   04,   50) -- note month
;

select distinct year, month, sum(delta) over(partition by year order by  month)
from (
    -- real delta
    select year, month, delta = qty_ytd - isnull(lag(qty_ytd) over (partition by year order by month),0)
    from #t
    union all
    -- tally dummy delta
    select top(24) 2017 + (n-1)/12, n%12 + 1 , 0
    from 
    ( select row_number() over(order by a.n) n
      from
      (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) a(n),
      (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) b(n)
    ) c
)d
order by year, month;

你的问题是什么?你已经说了你想要什么,但什么都没问。你试过什么?关于如何做到这一点,有很多例子。就我个人而言,我推荐一个理货表或日历表。在您编写的第一个查询中,我需要从select distinct year中添加m,从t y交叉连接中添加m,对吗?@csharp。不,那是一种类型。应该是,现在是v.m。在第一个查询中,我需要更改其中t.year=y.year和t.m
create table #t
(
 year int,
 month int, 
 qty_ytd int
);

insert #t(year, month, qty_ytd )
values
(2017,   01,   20),
(2017,   02,   30),
(2018,   04,   50) -- note month
;

select distinct year, month, sum(delta) over(partition by year order by  month)
from (
    -- real delta
    select year, month, delta = qty_ytd - isnull(lag(qty_ytd) over (partition by year order by month),0)
    from #t
    union all
    -- tally dummy delta
    select top(24) 2017 + (n-1)/12, n%12 + 1 , 0
    from 
    ( select row_number() over(order by a.n) n
      from
      (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) a(n),
      (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) b(n)
    ) c
)d
order by year, month;