SQL Server: how to populate sparse data with the rest of zero values?

馋奶兔 提交于 2019-12-06 12:07:24

Prior to pivoting to your crosstab format, you would cross join tables Customers and Months, and then left join table Sales to that.

select 
    c.CustomerId
  , m.MonthId
  , Value = isnull(s.Value,0)
from customers c
  cross join months m
  left join sales s
    on s.CustomerId = c.CustomerId
   and s.MonthId = m.MonthId

rextester demo: http://rextester.com/XKU62242

returns:

+------------+---------+-------+
| CustomerId | MonthId | Value |
+------------+---------+-------+
|          1 |  201101 |    10 |
|          2 |  201101 |   100 |
|          3 |  201101 |     0 |
|          4 |  201101 |     0 |
|          1 |  201102 |     0 |
|          2 |  201102 |     0 |
|          3 |  201102 |     0 |
|          4 |  201102 |     0 |
|          1 |  201103 |     0 |
|          2 |  201103 |     0 |
|          3 |  201103 |     0 |
|          4 |  201103 |     0 |
+------------+---------+-------+

Adding a dynamic pivot() to the above could be done like so:

declare @cols nvarchar(max);
declare @sql  nvarchar(max);

select @cols = stuff((
    select ',' + quotename(MonthId)
    from months 
    order by MonthId
    for xml path (''), type).value('.','nvarchar(max)')
  ,1,1,'');

select @sql = '
select CustomerId, ' + @cols + '
from (
    select 
        c.CustomerId
      , m.MonthId
      , Value = isnull(s.Value,0)
    from customers c
      cross join months m
      left join sales s
        on s.CustomerId = c.CustomerId
      and s.MonthId = m.MonthId
    ) as t
pivot (sum([Value]) for [MonthId] in (' + @cols + ') ) p';

select @sql as CodeGenerated;
exec sp_executesql @sql;

returns:

+-----------------------------------------------------------------------+
|                             CodeGenerated                             |
+-----------------------------------------------------------------------+
| select CustomerId, [201101],[201102],[201103]                         |
| from (                                                                |
|     select                                                            |
|         c.CustomerId                                                  |
|       , m.MonthId                                                     |
|       , Value = isnull(s.Value,0)                                     |
|     from customers c                                                  |
|       cross join months m                                             |
|       left join sales s                                               |
|         on s.CustomerId = c.CustomerId                                |
|       and s.MonthId = m.MonthId                                       |
|     ) as t                                                            |
| pivot (sum([Value]) for [MonthId] in ([201101],[201102],[201103]) ) p |
+-----------------------------------------------------------------------+

and the exec returns:

+------------+--------+--------+--------+
| CustomerId | 201101 | 201102 | 201103 |
+------------+--------+--------+--------+
|          1 |     10 |      0 |      0 |
|          2 |    100 |      0 |      0 |
|          3 |      0 |      0 |      0 |
|          4 |      0 |      0 |      0 |
+------------+--------+--------+--------+
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!