甲骨文。计算子组中一列的总和,并将其他列保存在查询结果中

时间:2016-06-06 11:26:32

标签: oracle11g group-by

我有像这样的table1。 id | name | sum1 | sum2 | bonus ———|——————|—————————|——————|—————— 9 | X | 225 | 0,68 | 3 10 | X | 30 | 0,85 | 3 11 | X | 3384,73 | 0,8 | 3 15 | Y | 2800 | 2 | 3 16 | Y | 500 | 0 | 0 17 | Y | 2077,49 | 0,8 | 3 18 | Y | 26736,96| 0,7 | 8 19 | Z | 209,9 | 1,5 | 3 20 | Z | 700 | 1 | 3 21 | Z | 6550 | 0 | 0

我希望每个"姓名"子组并获取结果查询表2 id | name | sum1 | sum2 | bonus ————————|——————| ————————|——————|—————— 9 | X | 225 | 0,68 | 3 10 | X | 30 | 0,85 | 3 11 | X | 3384,73 | 0,8 | 3 totalX | null | null | null | 9 15 | Y | 2800 | 2 | 3 16 | Y | 500 | 0 | 0 17 | Y | 2077,49 | 0,8 | 3 18 | Y | 26736,96| 0,7 | 8 totalY | null | null | null | 14 19 | Z | 209,9 | 1,5 | 3 20 | Z | 700 | 1 | 3 21 | Z | 6550 | 0 | 0 totalZ | null | null | null | 6

我确实通过"

尝试了#34; over
SELECT table1.*, sum(bonus)  over (PARTITION by name) as bonus_total FROM table1

它给了我一个额外的列,每个子组都有奖金总额,但这不是我想得到的 id | name | sum1 | sum2 | bonus| bonus_total ————————|——————| ————————|——————|——————|———————————— 9 | X | 225 | 0,68 | 3| 9 10 | X | 30 | 0,85 | 3| 9 11 | X | 3384,73 | 0,8 | 3| 9 15 | Y | 2800 | 2 | 3| 14 16 | Y | 500 | 0 | 0| 14 17 | Y | 2077,49 | 0,8 | 3| 14 18 | Y | 26736,96| 0,7 | 8| 14 19 | Z | 209,9 | 1,5 | 3| 6 20 | Z | 700 | 1 | 3| 6 21 | Z | 6550 | 0 | 0| 6

1 个答案:

答案 0 :(得分:1)

您可以通过累加部分组加上一些条件子句来执行此操作:

with table1 as (select 9 id, 'X' name, 225 sum1, 0.68 sum2, 3 bonus from dual union all
                select 10 id, 'X' name, 30 sum1, 0.85 sum2, 3 bonus from dual union all
                select 11 id, 'X' name, 3384.73 sum1, 0.8 sum2, 3 bonus from dual union all
                select 15 id, 'Y' name, 2800 sum1, 2 sum2, 3 bonus from dual union all
                select 16 id, 'Y' name, 500 sum1, 0 sum2, 0 bonus from dual union all
                select 17 id, 'Y' name, 2077.49 sum1, 0.8 sum2, 3 bonus from dual union all
                select 18 id, 'Y' name, 26736.96 sum1, 0.7 sum2, 8 bonus from dual union all
                select 19 id, 'Z' name, 209.9 sum1, 1.5 sum2, 3 bonus from dual union all
                select 20 id, 'Z' name, 700 sum1, 1 sum2, 3 bonus from dual union all
                select 21 id, 'Z' name, 6550 sum1, 0 sum2, 0 bonus from dual)
select   case when id is null then 'total'||name else to_char(id) end id,
         case when id is not null then name end name,
         case when id is not null then sum(sum1) end sum1,
         case when id is not null then sum(sum2) end sum2,
         sum(bonus) bonus
from     table1 t1
group by name, rollup (id)
order by t1.name, t1.id;

ID       NAME       SUM1       SUM2      BONUS
-------- ---- ---------- ---------- ----------
9        X           225        .68          3
10       X            30        .85          3
11       X       3384.73         .8          3
totalX                                       9
15       Y          2800          2          3
16       Y           500          0          0
17       Y       2077.49         .8          3
18       Y      26736.96         .7          8
totalY                                      14
19       Z         209.9        1.5          3
20       Z           700          1          3
21       Z          6550          0          0
totalZ                                       6

仅需要case语句来获取所需的格式。我必须在sum1和sum2列周围包含总和,以便让它们显示在你想要的结果中 - 我们将它们转换为输出的空值。

另外,我假设id列设置为禁止空值。