带有父子关系的递归计算

时间:2018-06-26 18:06:01

标签: sql sql-server parent-child common-table-expression recursive-query

有人可以帮助我如何计算父子关系。本质上Total2 =分配+ CalCfield + Total1 Total1 =孩子的SUM(Total2)

下面是用于复制数据的查询。临时表包含源数据和 finaloutput包含结果。我还附加了我现在拥有的查询,该查询无法产生正确的结果

-源数据

 CREATE TABLE temp ( [ParentCompany] VARCHAR, [ChildCompany] 
  VARCHAR,Allocation FLOAT, Factor FLOAT, CalcField FLOAT)

   INSERT INTO temp( [ParentCompany] , [ChildCompany] ,Allocation ,Factor, 
   CalcField )
  Values

  ('1',         NULL,     100,1,      2560),
  ('1',          '2'            ,100, 0.95 ,9396.537245),
  ('2',          NULL,     100,1,      1000),
  ('2',          '3',           100, 1.05, 10385.64643),
  ('3',          NULL,     100,1,      6000),
  ('3',          '4',           100,1       , 3891.091837),
  ('4',          NULL,     100,1,      2814),
  ('4',          '5'            ,   40 , 1.02   , 3968.913674),
  ('5',          NULL,     0             , 0.87, 4000),
  ('5',          '6'            ,100, 0.87 ,2836.956522),
  ('5',          '7'            ,100, 0.87 ,1626.168224),
  ('6',          NULL,     60            ,  0.92, 3000),
  ('7',          NULL,     100, 1.07, 2000)

-输出数据

CREATE TABLE finaloutput([ParentCompany] VARCHAR,[ChildCompany]    VARCHAR,分配浮点数,因子浮点数,CalcField浮点数    ,总计1个浮点数,总计2个浮点数)

 INSERT INTO finaloutput( [ParentCompany] , [ChildCompany] ,Allocation 
,Factor, 
 CalcField ,Total1, Total2)
 Values
 ('1',          NULL,     100, 1.05, 2560   , 81305.02 , 0),
 ('1',          '2'            ,100, 0.95 , 9396.537 , 71808.48  , 81305.02),

 ('2',          NULL,     100, 1.05, 1000 , 41147.06 , 0),
 ('2',          '3',           100, 1.05, 10385.646 , 30661.42 , 41147.06),

('3',          NULL,     100, 1 ,  6000      , 17326.26   ,  0),
('3',          '4',           100, 1      , 3891.091  ,  13335.16  ,  
17326.26),


('4',          NULL,     100,   1    , 2814     , 8672.038, 11586.038),
('4',          '5'            ,   40 , 1.02   , 3968.913 , 4663.12 , 
  8672.038),

 ('5',          NULL,     0             , 0.87, 4000,4663.12,0),
('5',          '6'            ,100, 0.87 ,2836.956,0,2936.956),
('5',          '7'            ,100, 0.87 ,1626.168,0,1726.168),
('6',          NULL,     60            ,  0.92, 3000,0,0),
('7',          NULL,     100, 1.07, 2000,0,0)

-查询我现在拥有的

  ;WITH CTE_TEST AS (
   Select ParentCompany,ChildCompany,Allocation,CalcField,
      CAST(0 AS FLOAT) Total2,
      CASE WHEN (count(ChildCompany) over (partition by ParentCompany)) = 0 
   AND ChildCompany IS NULL THEN CAST(0 AS FLOAT) END Total1,
      [Steps] = 0
  FROM Temp 
  WHERE ChildCompany IS NULL

  UNION ALL
  SELECT a.ParentCompany,a.ChildCompany,a.Allocation,a.CalcField,
    a.CalcField + a.Allocation+ b.Total1 AS Total2,
      b.total2 AS Total1,
      b.steps+1
  FROM Temp a 
  JOIN CTE_Test b on a.ChildCompany = b.ParentCompany)


  SELECT ParentCompany,ChildCompany,Allocation,CalcField,SUM(Total1) Total1, 
  SUM(Total2) Total2
  FROM CTE_TEST
  GROUP BY ParentCompany,ChildCompany,Allocation,CalcField
  ORDER by 1,2

0 个答案:

没有答案