【问题标题】:Sum when field does not exist字段不存在时求和
【发布时间】:2013-05-27 16:45:39
【问题描述】:

我有下表:

create table #tbl
(
  [type] varchar(20),
  [qty] int
)


insert into #tbl values ('Type A', 10)
insert into #tbl values ('Type A', 15)
insert into #tbl values ('Type B', 5)
insert into #tbl values ('Type B', 8)

现在我想显示每个“类型”的总数量:

select
 isnull([type], 'Other') as [type],
 sum(case 
  when [type] = 'Type A' then qty
  when [type] = 'Type B' then qty
  when [type] = 'Type C' then qty 
  else 0
 end) as [total]
from #tbl
where [type] in ('Type A', 'Type B', 'Type C')
group by [type]

它正确地总结了每个“类型”。结果如下:

type    total
--------------
Type A     25
Type B     13

但我希望 Type C 也包含在结果中(总数量为 0)。

type    total
--------------
Type A     25
Type B     13
Type C      0

我怎样才能做到这一点? 我正在使用 MS SQL Server 2005。

【问题讨论】:

    标签: sql sql-server sql-server-2008 tsql sql-server-2005


    【解决方案1】:

    问题是您的表中没有Type C,所以没有什么可以返回。您可以这样做的一种方法是创建一个包含您想要包含的所有值的派生表,然后 LEFT JOIN 您的表:

    select d.type,
      sum(coalesce(t.qty, 0)) Total
    from
    (
      select 'Type A' type union all
      select 'Type B' type union all
      select 'Type C' type 
    ) d
    left join tbl t
      on d.type = t.type
    group by d.type;
    

    SQL Fiddle with Demo

    【讨论】:

      【解决方案2】:

      您将需要一个包含您要报告的类型列表的表格,并对其进行左连接。类似于以下内容:

      create table #tbl
      (
        [type] varchar(20),
        [qty] int
      );
      
      insert into #tbl values ('Type A', 10)
      insert into #tbl values ('Type A', 15)
      insert into #tbl values ('Type B', 5)
      insert into #tbl values ('Type B', 8)
      
      create table #types ( [type] varchar(20) );
      
      insert into #types values ('Type A' );
      insert into #types values ('Type B' );
      insert into #types values ('Type C' );
      
      select  t.[type], [Total] = IsNull(t.[total], 0)
      from    (   select  [type] = IsNull(t.[Type], 'Other')
                  ,       [total] = sum(tbl.[qty])
                  from    #types                      t
                  left
                  join    #tbl                        tbl     ON  tbl.[type] = t.type
                  group
                  by      t.[type]
              ) as t
      ;
      

      子查询是将 NULL 和转换为零所必需的。

      【讨论】:

        【解决方案3】:

        您还可以通过应用 UNPIVOT 和 PIVOT 运算符来获得结果。

        SELECT type, qty
        FROM(
             SELECT COALESCE([Type A], 0) AS [Type A],
                    COALESCE([Type B], 0) AS [Type B],
                    COALESCE([Type C], 0) AS [Type C]
             FROM (
                   SELECT [type], [qty]
                   FROM #tbl
                   ) x
             PIVOT (
                    SUM([qty]) FOR [type] IN([Type A], [Type B], [Type C])
                    ) p
             )x
        UNPIVOT (
                 [qty] FOR [type] IN([Type A], [Type B], [Type C])
                 ) u
        

        SQLFiddle上的演示

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-15
          • 2014-10-24
          • 2020-03-10
          相关资源
          最近更新 更多