我试图通过汇总oracle函数使用一个组,但没有正确使用它。
这是我的数据格式(L1_Proj_ID
是1级项目ID等.....)
Proj_ID Hours_Charged L1_Proj_ID L2_Proj_ID L3_Proj_ID L4_Proj_ID
-------------------------------------------------------------------------
1100.10 20 1100 1100.10 Null Null
1100.11.01 30 1100 1100.11 1100.11.01 Null
1100.11.02 40 1100 1100.11 1100.11.02 Null
1100.12.01.01 50 1100 1100.12 1100.12.01 1100.12.01.01
我需要得到累计总数,我的输出应该是
Proj_Level Hours_Charged
--------------------------
1100 140
1100.10 20
1100.11 70
1100.11.01 30
1100.11.02 40
1100.12 50
1100.12.01 50
1100.12.01.01 50
请告诉我是否还有其他简单方法。
截至目前,我可以获得这样的数据......
select
L1_proj_id,
sum(hours_charged) as hours_charged
from table
group by
l1_proj_id
union all
select
L2_proj_id,
sum(hours_charged) as hours_charged
from table
group by
21_proj_id
union all
select
L3_proj_id,
sum(hours_charged) as hours_charged
from table
group by
l3_proj_id
union all
select
L4_proj_id,
sum(hours_charged) as hours_charged
from table
group by
l4_proj_id
答案 0 :(得分:1)
这不使用rollup
,但我认为它可能会带来您的结果。从本质上讲,我将您的列排除在行之外。如果您的内容发生变化,它也应该相对易于扩展。
with levels as (
select level id
from dual
connect by level <= 4
),
all_data as (
select
case l.id
when 1 then l1_proj_id
when 2 then l2_proj_id
when 3 then l3_proj_id
when 4 then l4_proj_id
end as project_id,
t.hours_charged
from
table t,
levels l
)
select
project_id, sum (hours_charged) as hours_charged
from all_data
where project_id is not null
group by project_id