分层查询子覆盖

问题描述 投票:2回答:1

我有一个分层的公司结构,每个公司都有一个可选的公司部门。儿童公司如果没有自己的母公司继承其母公司的部门,但子公司部门如果是子公司则优先于其母公司。

我需要产生按部门分组的账单金额的总和。没有部门的公司会被忽视。

Company表中的ParentCompanyID列是自身的外键。可以有无限数量的父/子级别。

公司表

CompanyID ParentCompanyID
   1           null
   2            1
   3            2
   4           null
   5            4
   6           null
   7            6
   8            7

CompanySector表

CompanyID    Sector
    1     Distribution
    4     Distribution
    5     Manufacturing
    8     Manufacturing

时间表

CompanyID  BillAmount
    1        100
    1        200
    2        100
    3        50
    4        25
    5        75
    6        75
    7        115
    8        115

我希望在这里看到的结果是:

   Sector      BillAmount 
Distribution      475
Manufacturing     190

以下是我目前的做法。它效率极低,不适用于无限数量的层次级别。

WITH Company AS
  ( SELECT 1 CompanyID, NULL ParentCompanyID FROM dual
  UNION
  SELECT 2, 1 FROM dual
  UNION
  SELECT 3, 2 FROM dual
  UNION
  SELECT 4, NULL FROM dual
  UNION
  SELECT 5, 4 FROM dual
  UNION
  SELECT 6, NULL FROM dual
  UNION
  SELECT 7, 6 FROM dual
  UNION
  SELECT 8, 7 FROM dual
  ),
  CompanySector AS
  ( SELECT 1 CompanyID, 'Distribution' Sector FROM dual
  UNION
  SELECT 4 , 'Distribution' FROM dual
  UNION
  SELECT 5 , 'Manufacturing' FROM dual
  UNION
  SELECT 8 , 'Manufacturing' FROM dual
  ),
  Timesheets AS
  ( SELECT 1 CompanyID, 100 BillAmount FROM dual
  UNION
  SELECT 1 CompanyID, 200 BillAmount FROM dual
  UNION
  SELECT 2 CompanyID, 100 BillAmount FROM dual
  UNION
  SELECT 3 CompanyID, 50 BillAmount FROM dual
  UNION
  SELECT 4 CompanyID, 25 BillAmount FROM dual
  UNION
  SELECT 5 CompanyID, 75 BillAmount FROM dual
  UNION
  SELECT 6 CompanyID, 75 BillAmount FROM dual
  UNION
  SELECT 7 CompanyID, 115 BillAmount FROM dual
  UNION
  SELECT 8 CompanyID, 115 BillAmount FROM dual
  ),

  --Dummy tables above
  --My current way of doing it below
  companies AS
  (SELECT c.*,
    cs.sector
  FROM company c
  LEFT OUTER JOIN CompanySector cs
  ON c.companyID = cs.companyID
  ),
  sectors AS
  (SELECT levelOne.companyID,
    NVL(levelOne.sector, NVL(levelTwo.sector, NVL(levelThree.sector, NULL))) sector
  FROM companies levelOne
  LEFT OUTER JOIN companies levelTwo
  ON levelOne.parentcompanyid = levelTwo.companyID
  LEFT OUTER JOIN companies levelThree
  ON levelTwo.parentcompanyid                                                     = levelThree.companyID
  WHERE NVL(levelOne.sector, NVL(levelTwo.sector, NVL(levelThree.sector, NULL))) IS NOT NULL
  )
SELECT s.sector,
  SUM(t.billamount)
FROM sectors s
INNER JOIN timesheets t
ON s.companyID = t.companyID
GROUP BY sector;

什么是更清洁,更有效的方法?

sql oracle hierarchical-data
1个回答
1
投票

我相信这会做到这一点。使用分层查询语法,在需要的地方填充父记录中的扇区。

WITH
  base_sectors AS (
  SELECT * FROM company LEFT OUTER JOIN companySector USING (companyID)
  )
, final_sectors AS (
  SELECT companyID, NVL( sector, PRIOR sector ) AS sector
  FROM base_sectors
  START WITH parentCompanyID IS NULL
  CONNECT BY parentCompanyID = PRIOR companyID
)
SELECT s.sector,
  SUM(t.billamount)
FROM final_sectors s
INNER JOIN timesheets t
ON s.companyID = t.companyID
GROUP BY sector;
© www.soinside.com 2019 - 2024. All rights reserved.