gpt4 book ai didi

sql - 甲骨文 SQL : How to add multi sub total in sql?

转载 作者:行者123 更新时间:2023-12-04 23:47:50 26 4
gpt4 key购买 nike

我想在查询sql中查询多个小计。如何查询多小计。请帮助我。

SQL 代码:

SELECT MB.COUNTRY_CODE As C_CODE,
MB.OPERATOR_NAME AS C_NAME,
C.PMN_CODE AS OPER_NAME,
(C.RANG_START / 1024) AS R_START,
(C.RANG_END / 1024) AS R_END,
COUNT(*) AS T_OPER,
'0' AS TE_CHARGE,
SUM(C.RATE) AS TR_CHARGE
FROM IR_V_IMM_RATE_BY_CDR C
LEFT JOIN IR_PMN_MASTER_BROKER MB ON C.COMPANY_CODE = MB.COMPANY_CODE
AND C.PMN_DOMAIN = MB.PMN_CODE
LEFT JOIN IR_PMN_INTER_SERVICE S ON C.PMN_DOMAIN = S.PMN_CODE
AND C.COMPANY_CODE = S.COMPANY_CODE
WHERE C.SUB_EVENT_TYPE_ID = '2'
AND C.MODIFIER = 'P1P'
AND S.INTER_SERVICE = 'MSS'
AND C.COMPANY_CODE = 'ASS'
AND C.EVENT_TYPE_ID = '622'
AND C.PMN_DOMAIN = 'ACN'
AND TO_CHAR(C.CUTOFF_DATE, 'MM/YYYY') = '04/2015'
GROUP BY MB.COUNTRY_CODE, MB.OPERATOR_NAME, C.PMN_CODE, C.RANG_START, C.RANG_END, C.RATE

结果查询:

    C_CODE | C_NAME | OPER_NAME | R_START | R_END | T_OPER | TE_CHARGE | TR_CHARGE
1 | Aic Ltd| AA | 1 | 10 | 1 | 0 | 0.8324
1 | Aic Ltd| AA | 3 | 22 | 5 | 0 | 2.312931
1 | Aic Ltd| BB | 4 | 20 | 2 | 0 | 10
1 | Aic Ltd| BB | 1 | 13 | 1 | 0 | 25

但我想在查询中添加多个小计。

我要查询结果:

C_CODE | C_NAME | OPER_NAME | R_START | R_END | T_OPER | TE_CHARGE | TR_CHARGE
1 | Aic Ltd| AA | 1 | 10 | 1 | 0 | 0.8324
1 | Aic Ltd| AA | 3 | 22 | 5 | 0 | 2.312931
| Total | 6 | 0 | 3.145331
------------------------------------------------------------------------------
1 | Aic Ltd| BB | 4 | 20 | 2 | 0 | 10
1 | Aic Ltd| BB | 1 | 13 | 1 | 0 | 25
| Total | 3 | 0 | 35
------------------------------------------------------------------------------
Total | 9 | 0 | 38.145331
------------------------------------------------------------------------------
6 | SRI | CC | 2 | 11 | 2 | 0 | 1
6 | SRI | CC | 4 | 21 | 1 | 0 | 5
| Total | 3 | 0 | 6
--------------------------------------------------------------------------------
6 | SRI | DD | 5 | 30 | 1 | 0 | 1.21
6 | SRI | DD | 2 | 40 | 1 | 0 | 2.35
| Total | 2 | 0 | 5.42
--------------------------------------------------------------------------------
Total | 5 | 0 | 11.42
--------------------------------------------------------------------------------

如何添加多行小计?请帮助我。

提前致谢。 ;)

最佳答案

我将发布一个使用 group by rollup extension 的解决方案.请试试这个。

with Src as (
select 1 C_CODE, 'Aic Ltd' C_NAME, 'AA' OPER_NAME, 1 R_START, 10 R_END, 1 T_OPER, 0 TE_CHARGE, 0.8324 TR_CHARGE from dual union all
select 1 C_CODE, 'Aic Ltd' C_NAME, 'AA' OPER_NAME, 3 R_START, 22 R_END, 5 T_OPER, 0 TE_CHARGE, 2.312931 TR_CHARGE from dual union all

select 1 C_CODE, 'Aic Ltd' C_NAME, 'BB' OPER_NAME, 4 R_START, 20 R_END, 2 T_OPER, 0 TE_CHARGE, 10 TR_CHARGE from dual union all
select 1 C_CODE, 'Aic Ltd' C_NAME, 'BB' OPER_NAME, 1 R_START, 13 R_END, 1 T_OPER, 0 TE_CHARGE, 25 TR_CHARGE from dual union all

select 6 C_CODE, 'SRI' C_NAME, 'CC' OPER_NAME, 2 R_START, 11 R_END, 2 T_OPER, 0 TE_CHARGE, 1 TR_CHARGE from dual union all
select 6 C_CODE, 'SRI' C_NAME, 'CC' OPER_NAME, 4 R_START, 21 R_END, 1 T_OPER, 0 TE_CHARGE, 5 TR_CHARGE from dual union all

select 6 C_CODE, 'SRI' C_NAME, 'DD' OPER_NAME, 5 R_START, 30 R_END, 1 T_OPER, 0 TE_CHARGE, 1.21 TR_CHARGE from dual union all
select 6 C_CODE, 'SRI' C_NAME, 'DD' OPER_NAME, 2 R_START, 40 R_END, 1 T_OPER, 0 TE_CHARGE, 2.35 TR_CHARGE from dual
)

select c_code, c_name, oper_name, r_start, R_End, sum(T_OPER), sum(TE_CHARGE),
sum(tr_charge)
from src
group by rollup(c_code, c_name, oper_name, r_start, R_End)
having grouping(c_name) = 0 and (grouping(r_Start) = 1 or grouping(R_End) = 1)

虽然我认为提供了答案Gordon Linoff指出也是对这个问题很好的回答和建议。

关于sql - 甲骨文 SQL : How to add multi sub total in sql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com