【问题标题】:Hierarchy columns Aggregation on a single measure with sql使用 sql 在单个度量上聚合层次结构列
【发布时间】:2016-06-27 23:34:23
【问题描述】:

Data in Tabular formi 在我的表中有 5 个层次结构,并且想根据层次结构计算度量值列。请输入任何内容。问题是如果我选择 level1 和 sum(Value) 那么我应该得到 Level1 的总数。如果我选择 level2 和 sum(Value) 那么我应该得到 level2 的总数。如果我拉 Level5 和 sum(Value),那么我应该得到 level5 的总数。 Level1总到Level15总和level1是parent,2,3,4,5是children。

下面是我的插入sql

CREATE TABLE HTable (
    LEVEL1 VARCHAR(10) ,
    LEVEL2 VARCHAR(28) ,
    LEVEL3 VARCHAR(17) ,
    LEVEL4 VARCHAR(26) ,
    LEVEL5 VARCHAR(9) ,
    HIERARCHY_ID INT,
    Value INT
);
INSERT INTO HTable VALUES ('All Groups','Miscellaneous Service Groups','DISP','NULL','NULL',138166,NULL);
INSERT INTO HTable VALUES ('All Groups','Miscellaneous Service Groups','Help Desk','NULL','NULL',138165,NULL);
INSERT INTO HTable VALUES ('All Groups','Miscellaneous Service Groups','IT Plan and Admin','NULL','NULL',138168,NULL);
INSERT INTO HTable VALUES ('All Groups','Miscellaneous Service Groups','k900XGA01','NULL','NULL',138162,NULL);
INSERT INTO HTable VALUES ('All Groups','Miscellaneous Service Groups','Surplus','NULL','NULL',138163,NULL);
INSERT INTO HTable VALUES ('All Groups','Miscellaneous Service Groups','TREES','NULL','NULL',138167,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NDS','Firewall / VPN Management','k900_GD04',48061,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NDS','Firewall / VPN Management','k900_GD06',48062,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NDS','MetroNet','k900_GD02',48058,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NDS','Router Management','k900_GD03',48060,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NDS','Total QSO Internet','k900_GD01',48056,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NDS','Total QSO Internet','k900_GD07',48057,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NDS','Total WAN','k900_GD05',48066,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NVS','Local Services Telco','k900_GB06',48054,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NVS','Long Distance Telco','k900_GB07',48055,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NVS','Shared CENTREX','k900_GB03',48051,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NVS','Shared CENTREX','k900_GB10',48052,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NVS','Telecom Project Management','k900_GB01',48049,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NVS','Telecom Project Management','k900_GB08',133656,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NVS','Telecom Wiring','k900_GB02',48050,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NVS','Telecom Wiring','k900_GB09',133659,NULL);
INSERT INTO HTable VALUES ('All Groups','Network Services','NVS','Total VoiP','k900_GB05',48053,NULL);
INSERT INTO HTable VALUES ('All Groups','Print Services Group','k900_GZ00','NULL','NULL',140641,NULL);
INSERT INTO HTable VALUES ('All Groups','Print Services Group','TPS','D500_GK00','NULL',48095,NULL);
INSERT INTO HTable VALUES ('All Groups','Print Services Group','TPS','D500_GK00','NULL',48108,NULL);

【问题讨论】:

  • 编辑您的问题并描述您想要执行的处理。期望的结果会有所帮助,表格格式的样本数据也会有所帮助。
  • 您的问题是什么,您尝试过什么?
  • 戈登,我已经以标签形式添加了数据。谢谢...埃里克,我已经详细说明了这个问题..谢谢

标签: sql sql-server hierarchical-data


【解决方案1】:

我能够创建一个动态范围键并使用您的数据派生一个伪层次结构。提供的数据不是 TRUE 层次结构,我还创建了一些随机值来说明聚合

以下是您的基础数据

SQL

;with cteBase as (
    Select RowNr=cast(Row_Number() over (Order By Level1,Level2,Level3,Level4,Level5) as money),* from HTable
   ),
   cteRange as (
       Select Lvl=1,R1=min(RowNr),R2=max(RowNr),Title=Level1 From cteBase Where Level1 is not null Group By Level1
       Union All
       Select Lvl=2,R1=min(RowNr)+.02,R2=max(RowNr),Title=Level2 From cteBase Where Level2 is not null Group By Level2
       Union All
       Select Lvl=3,R1=min(RowNr)+.03,R2=max(RowNr),Title=Level3 From cteBase Where Level3 is not null Group By Level3
       Union All
       Select Lvl=4,R1=min(RowNr)+.04,R2=max(RowNr),Title=Level4 From cteBase Where Level4 is not null Group By Level4
       Union All
       Select Lvl=5,R1=min(RowNr)+.05,R2=max(RowNr),Title=Level5 From cteBase Where Level5 is not null Group By Level5
   )
   Select Lvl
         ,R1
         ,R2
         ,Title = Replicate('   ',Lvl-1)+Title
         ,Value = sum (B.Value)
   from cteRange A
   Join cteBase B on B.RowNr between Floor(A.R1) and A.R2
   Where  Lvl<55
   Group By 
          Lvl
         ,R1
         ,R2
         ,Title
  Order By R1

结果是:

【讨论】:

    【解决方案2】:

    检查以下查询是否有用

    SELECT Level1
        ,Level2
        ,level3
        ,level4
        ,level5
        ,sum(ISNULL(value, 0)) AS sum_val
    FROM #HTable
    GROUP BY ROLLUP(Level1, Level2, level3, level4, level5)
    ORDER BY Level1
        ,Level2
        ,level3
        ,level4
        ,level5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多