多层层次结构的小计和总计

时间:2019-11-12 03:33:11

标签: sql-server sql-server-2012

我正在设置报告查询。我有多个位置层次结构,需要正确显示。

我尝试使用按组汇总,但无法获得所需的内容。

这是我的数据样本。

TblValues

Branch  BranchName  Cluster Location    District    Value
0001      A           C1      Loc1         District1    1000
0002      B           C1      Loc1         District1    2000
0003      C           C2      Loc2         District1    3000
0004      D           C2      Loc2         District1    4000
0005      E           C3      Loc2         District1    5000

这是我到目前为止尝试过的。

SELECT  CASE WHEN GROUPING(a.cluster) = 1 THEN a.District
             WHEN GROUPING(a.branchname) = 1 THEN a.Cluster
             ELSE a.BranchName
        END ,
        SUM(a.Value) Value
FROM    ( SELECT    '0001' Branch ,
                    'A' BranchName ,
                    'C1' Cluster ,
                    'Loc1' Location ,
                    'District1' District ,
                    1000 Value
          UNION ALL
          SELECT    '0002' ,
                    'B' ,
                    'C1' ,
                    'Loc1' ,
                    'District1' ,
                    2000
          UNION ALL
          SELECT    '0003' ,
                    'C' ,
                    'C2' ,
                    'Loc2' ,
                    'District1' ,
                    3000
          UNION ALL
          SELECT    '0004' ,
                    'D' ,
                    'C2' ,
                    'Loc2' ,
                    'District1' ,
                    4000
          UNION ALL
          SELECT    '0005' ,
                    'E' ,
                    'C3' ,
                    'Loc2' ,
                    'District1' ,
                    5000
        ) a
GROUP BY ROLLUP(a.Cluster, a.BranchName) ,
        a.District;

这是我的结果。

Loc Value
A   1000
B   2000
C1  3000
C   3000
D   4000
C2  7000
E   5000
C3  5000
District1   15000

我也想显示位置。像这样:

Loc Value
A   1000
B   2000
C1  3000
Loc1    3000
C   3000
D   4000
C2  7000
E   5000
C3  5000
Loc2    12000
District1   15000

2 个答案:

答案 0 :(得分:0)

使用Union ALL

FIDDLE DEMO

   SELECT BranchName, Value FROM
   (
        SELECT * FROM table
   ) A
   UNION ALL
   SELECT Location, SUM(Value) FROM
   (
        SELECT * FROM table
   ) A GROUP BY Location
   UNION ALL
   SELECT Cluster, SUM(Value) FROM
   (
        SELECT * FROM table
   ) A GROUP BY Cluster
   UNION ALL
   SELECT District, SUM(Value) FROM
   (
        SELECT * FROM table
   ) A GROUP BY District

答案 1 :(得分:0)

declare @tbl as table (Branch varchar(max), 
                        BranchName varchar(max),
                        Cluster varchar(max),
                        Location varchar(max),
                        District varchar(max),
                        Value int)

INSERT INTO @tbl
select '0001' ,'A','C1','Loc1', 'District1',1000 UNION ALL
select '0002' ,'B','C1','Loc1', 'District1',2000 UNION ALL
select '0003' ,'C','C2','Loc2', 'District1' ,3000 UNION ALL
select '0004' ,'D','C2','Loc2', 'District1' ,4000 UNION ALL
select '0005' ,'E','C3','Loc2', 'District1' ,5000


;with cte as (SELECT case when BranchName is null and cluster is not null then Cluster
            when BranchName is null and cluster is null and Location is not null then Location
            when BranchName is null and cluster is null and Location is null and District is not null then District else BranchName end Loc
, Cluster, Location, District,  sum(Value) Value FROM @tbl
group by
    grouping sets (
        (District),
        (Location, District),
        (Cluster,Location, District),
        (BranchName, Cluster, Location, District),
        (Value)))

select Loc, value from cte where Loc is not null