Select parent 行并包括 children 的成本
Select parent rows and include cost of children
我有一份工作单 table,其中包含 parent 和 child 工作单:
with workorder as (
select 'WO37342' as wonum, null as parent, 297.36 as actlabcost, 200 as actmatcost, 0 as actservcost, 0 as acttoolcost from dual
union all
select 'WO37427' as wonum, 'WO37342' as parent, 99.12 as actlabcost, 0 as actmatcost, 0 as actservcost, 0 as acttoolcost from dual
union all
select 'WO37429' as wonum, 'WO37342' as parent, 99.12 as actlabcost, 100 as actmatcost, 0 as actservcost, 0 as acttoolcost from dual
)
select
*
from
workorder
WONUM PARENT ACTLABCOST ACTMATCOST ACTSERVCOST ACTTOOLCOST
------- ------- ---------- ---------- ----------- -----------
WO37342 297.36 200 0 0
WO37427 WO37342 99.12 0 0 0
WO37429 WO37342 99.12 100 0 0
我想要 select parent 行并在 parent 中包含 children 的成本:
WONUM ACTLABCOST ACTMATCOST ACTSERVCOST ACTTOOLCOST
------- ----------- ---------- ----------- -----------
WO37342 495.6 300 0 0
在 Oracle 19c 中是否有一种简洁的方法可以做到这一点?
(我的目标是 SQL 尽可能 simple/readable。)
对于 one-level parent/children 关系,如示例数据所示,我建议:
select
coalesce(parent, wonum) wonum
sum(actlabcost) actlabcost,
sum(actmatcost) actmatcost,
sum(actservcost) actservcost,
sum(acttoolcost) acttoolcost
from workorder wo
group by coalesce(parent, wonum)
对于具有多个级别的层次结构,您可以使用 CONNECT_BY_ROOT( ... )
然后 GROUP BY
即:
SELECT root_wonum AS wonum,
SUM( actlabcost ) AS total_actlabcost,
SUM( actmatcost ) AS total_actmatcost,
SUM( actservcost ) AS total_actservcost,
SUM( acttoolcost ) AS total_acttoolcost
FROM (
SELECT CONNECT_BY_ROOT( wonum ) AS root_wonum,
actlabcost,
actmatcost,
actservcost,
acttoolcost
FROM workorder
START WITH parent IS NULL
CONNECT BY PRIOR wonum = parent
)
GROUP BY root_wonum;
其中,对于测试数据:
CREATE TABLE workorder ( wonum, parent, actlabcost, actmatcost, actservcost, acttoolcost ) as
select 'WO37342', null, 297.36, 200, 0, 0 from dual union all
select 'WO37427', 'WO37342', 99.12, 0, 0, 0 from dual union all
select 'WO37429', 'WO37342', 99.12, 100, 0, 0 from dual;
输出:
WONUM | TOTAL_ACTLABCOST | TOTAL_ACTMATCOST | TOTAL_ACTSERVCOST | TOTAL_ACTTOOLCOST
:------ | ---------------: | ---------------: | ----------------: | ----------------:
WO37342 | 495.6 | 300 | 0 | 0
db<>fiddle here
我有一份工作单 table,其中包含 parent 和 child 工作单:
with workorder as (
select 'WO37342' as wonum, null as parent, 297.36 as actlabcost, 200 as actmatcost, 0 as actservcost, 0 as acttoolcost from dual
union all
select 'WO37427' as wonum, 'WO37342' as parent, 99.12 as actlabcost, 0 as actmatcost, 0 as actservcost, 0 as acttoolcost from dual
union all
select 'WO37429' as wonum, 'WO37342' as parent, 99.12 as actlabcost, 100 as actmatcost, 0 as actservcost, 0 as acttoolcost from dual
)
select
*
from
workorder
WONUM PARENT ACTLABCOST ACTMATCOST ACTSERVCOST ACTTOOLCOST
------- ------- ---------- ---------- ----------- -----------
WO37342 297.36 200 0 0
WO37427 WO37342 99.12 0 0 0
WO37429 WO37342 99.12 100 0 0
我想要 select parent 行并在 parent 中包含 children 的成本:
WONUM ACTLABCOST ACTMATCOST ACTSERVCOST ACTTOOLCOST
------- ----------- ---------- ----------- -----------
WO37342 495.6 300 0 0
在 Oracle 19c 中是否有一种简洁的方法可以做到这一点?
(我的目标是 SQL 尽可能 simple/readable。)
对于 one-level parent/children 关系,如示例数据所示,我建议:
select
coalesce(parent, wonum) wonum
sum(actlabcost) actlabcost,
sum(actmatcost) actmatcost,
sum(actservcost) actservcost,
sum(acttoolcost) acttoolcost
from workorder wo
group by coalesce(parent, wonum)
对于具有多个级别的层次结构,您可以使用 CONNECT_BY_ROOT( ... )
然后 GROUP BY
即:
SELECT root_wonum AS wonum,
SUM( actlabcost ) AS total_actlabcost,
SUM( actmatcost ) AS total_actmatcost,
SUM( actservcost ) AS total_actservcost,
SUM( acttoolcost ) AS total_acttoolcost
FROM (
SELECT CONNECT_BY_ROOT( wonum ) AS root_wonum,
actlabcost,
actmatcost,
actservcost,
acttoolcost
FROM workorder
START WITH parent IS NULL
CONNECT BY PRIOR wonum = parent
)
GROUP BY root_wonum;
其中,对于测试数据:
CREATE TABLE workorder ( wonum, parent, actlabcost, actmatcost, actservcost, acttoolcost ) as
select 'WO37342', null, 297.36, 200, 0, 0 from dual union all
select 'WO37427', 'WO37342', 99.12, 0, 0, 0 from dual union all
select 'WO37429', 'WO37342', 99.12, 100, 0, 0 from dual;
输出:
WONUM | TOTAL_ACTLABCOST | TOTAL_ACTMATCOST | TOTAL_ACTSERVCOST | TOTAL_ACTTOOLCOST :------ | ---------------: | ---------------: | ----------------: | ----------------: WO37342 | 495.6 | 300 | 0 | 0
db<>fiddle here