【发布时间】:2015-10-22 16:41:12
【问题描述】:
我正在尝试使用 group by rollup 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
【问题讨论】:
-
你的数据结构很奇怪。 ROLLUP(col1, col2) 为您提供以下汇总结果: GROUP BY col1, col2;按 col1 分组;没有 GROUP BY。但是,您的 id 位于 5 个不同的列中。
-
@NeriaNachum 到目前为止,我可以通过 l1_proj_id 从表组中选择 L1_Proj_ID,求和(hours_charged)Hours_charges union all select L2_Proj_ID,从表组中求和(hours_charged)Hours_charges by l2_proj_id union all select L3_Proj_ID , sum (hours_charged) Hours_charges from table group by l3_proj_id union all select L4_Proj_ID, sum (hours_charged) Hours_charges from table group by l4_proj_id
-
我就是这么想的。它不漂亮,但我不完全理解为什么你有 5 个 id 的列代表相同的 id(?)。例如,与其使用这样的一行:1100.10 20 1100 1100.10,不如使用这样的三列:1100.10 20; 1100 20; 1100.10 20;