即使当月没有数据,也可以获得一行数据

时间:2017-03-07 12:15:19

标签: sql sql-server tsql

假设我有一份销售报告:

Salesman | Sales | Month  | yr |
    A        5      1       2016
    A        12     1       2017
    A        3      1       2017
    A        25     5       2017
    B        20     4       2017
    B        49     6       2017

如果我想看销售总销售员,我会做这样的事情:

SELECT salesman, sum(SALES), MONTH from SALESTABLE GROUP BY Salesman, Mnth   where year = 2017

得到:

Salesman | Sales | Mnth | yr |
   A        15       1    2017
   A        25       5    2017
   B        20       4    2017
   B        49       6    2017

但是我想要以下内容:

Salesman | Sales | Mnth | yr   |
   A         15     1     2017
   A         0      2     2017
   A         0      3     2017
   ...
   A         25     5     2017
   ...

我尝试了以下内容:

DECLARE @MonthTable TABLE
(
Monthnumber int
)


DECLARE @cnt int = 0
while @cnt < 12
BEGIN
  SET @cnt = @cnt+1
  Insert into @MonthTable (monthnumber) values (@cnt)
END


SELECT salesman, 
    sum(SALES), 
    monthnumber 
FROM SALESTABLE
RIGHT JOIN @MonthTable ON monthnumber = mnth    
GROUP BY Salesman, monthnumber

然而,正如预期的那样,这会在“推销员”上提供NULL

2 个答案:

答案 0 :(得分:3)

通过生成一个月份列表并交叉加入一个不同的销售人员列表和年份来加入表格,我们可以按生成的月份进行分组和求和:

;with months as (
  select Mnth 
  from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) t(Mnth)
)
select 
    s.Salesman
  , s.Yr
  , m.Mnth
  , Sales = coalesce(sum(t.Sales),0)
from (
  select distinct Salesman, yr
  from t
  ) as s
  cross join months m
  left join t
    on s.Salesman = t.Salesman
    and s.Yr = t.Yr
    and m.Mnth = t.[Month]
where s.yr = 2017
group by s.Salesman, s.Yr, m.Mnth

rextester演示:http://rextester.com/ZOHB53144

返回:

+----------+------+------+-------+
| Salesman |  Yr  | Mnth | Sales |
+----------+------+------+-------+
| A        | 2017 |    1 |    15 |
| A        | 2017 |    2 |     0 |
| A        | 2017 |    3 |     0 |
| A        | 2017 |    4 |     0 |
| A        | 2017 |    5 |    25 |
| A        | 2017 |    6 |     0 |
| A        | 2017 |    7 |     0 |
| A        | 2017 |    8 |     0 |
| A        | 2017 |    9 |     0 |
| A        | 2017 |   10 |     0 |
| A        | 2017 |   11 |     0 |
| A        | 2017 |   12 |     0 |
| B        | 2017 |    1 |     0 |
| B        | 2017 |    2 |     0 |
| B        | 2017 |    3 |     0 |
| B        | 2017 |    4 |    20 |
| B        | 2017 |    5 |     0 |
| B        | 2017 |    6 |    49 |
| B        | 2017 |    7 |     0 |
| B        | 2017 |    8 |     0 |
| B        | 2017 |    9 |     0 |
| B        | 2017 |   10 |     0 |
| B        | 2017 |   11 |     0 |
| B        | 2017 |   12 |     0 |
+----------+------+------+-------+

答案 1 :(得分:0)

正如您正确地创建了一个月份表来为那些没有销售的月份提供月份条目,您需要一个主销售员表来为销售人员提供有关那些月份的信息。有销售。

最简单的形式是,您可以通过salesdatadistinct salesman表生成一个主推销员表;理想情况下,您可能希望使用单独的销售人员表,每个销售人员有一个条目。

然后查询变为:

SELECT salesman, 
    sum(SALES), 
    monthnumber 
FROM SALESTABLE
RIGHT JOIN @MonthTable ON monthnumber = mnth    
GROUP BY Salesman, monthnumber

SELECT 
    salesmanlist.salesman 
  , SUM(salestable.sales) 
  , monthtable.monthnumber 
FROM 
  (SELECT DISTINCT salesman FROM salestable) as salesmanlist   --   <-- Key change 
  CROSS JOIN @MonthTable as monthtable 
  LEFT JOIN salestable 
    ON     salesmanlist.salesman = salestable.salesman 
       AND salestable.mnth = monthtable.monthnumber