Oracle Sql:如何在 sql 中添加多个子总计?

Posted

技术标签:

【中文标题】Oracle Sql:如何在 sql 中添加多个子总计?【英文标题】:Oracle Sql: How to add multi sub total in sql? 【发布时间】:2015-05-20 14:08:01 【问题描述】:

我想在查询 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
--------------------------------------------------------------------------------

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

提前致谢。 ;)

【问题讨论】:

【参考方案1】:

我将使用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指出的答案也是对这个问题非常好的答案和建议。

【讨论】:

【参考方案2】:

您需要的功能是使用GROUP BY 的扩展来提供的。因为你只想要一些小计,GROUPING SETS 是合适的。

GROUP BY GROUPING_SETS((MB.COUNTRY_CODE, MB.OPERATOR_NAME, C.PMN_CODE, C.RANG_START, C.RANG_END, C.RATE),
                       (MB.COUNTRY_CODE, MB.OPERATOR_NAME, C.PMN_CODE),
                       ())

这在documentation中有解释。

要在Oper_Name 列中获取诸如'Total' 之类的值,您需要标识额外的聚合行。 Oracle为此提供了GROUPING()函数:

(CASE WHEN GROUPING(C.PMN_CODE) = 1 THEN 'Total' ELSE C.PMN_CODE
 END) as Oper_Name

【讨论】:

错误 ORA 00907:这一行 .GROUPING_SETS((MB.COUNTRY_CODE

以上是关于Oracle Sql:如何在 sql 中添加多个子总计?的主要内容,如果未能解决你的问题,请参考以下文章

如何项sql数据库中添加一年的日期

如何使用poi在excel中添加多选下拉菜单

如何在 Android 的警报对话框中添加多项选择复选框?

如何在 Oracle SQL 中选择一个子字符串直到一个特定的字符?

sql 表中添加一列并将这一列赋值?

在放大graphql中添加多对多连接