Oracle Sql 查询:如何在包中旋转函数 Sum()?

Posted

技术标签:

【中文标题】Oracle Sql 查询:如何在包中旋转函数 Sum()?【英文标题】:Oracle Sql Query : How Pivot Function Sum() in Packages? 【发布时间】:2015-05-04 15:40:51 【问题描述】:

我使用 Oracle 10g 并在 Packages 中查询。如何在 Packages 中透视函数 sum()?

Sql 查询。

WITH DATA_JOIN AS
 (SELECT S.START_DATE_GMT,
         S.NO_OF_CALL,
         S.RATE,
         T.TAX_RATE,
         T.EFF_DATE,
         T.EXP_DATE,
         S.COMPANY_CODE,
         T.COMPANY_CODE CC,
         S.SUB_EVENT_TYPE_ID,
         S.event_type_id,
         S.PMN_ORIGIN,
         S.PMN_ROUTE
    FROM IR_V_ISM_RATE_BY_TRANS S
    LEFT JOIN IR_PMN_TAX T
      ON S.COMPANY_CODE = T.COMPANY_CODE
     AND S.PMN_ROUTE = T.PMN_CODE
   WHERE T.TAX_TYPE = '01'
     AND S.START_DATE_GMT <= T.EFF_DATE
     AND S.START_DATE_GMT >= T.EXP_DATE
      OR T.EXP_DATE IS NULL
   ORDER BY COMPANY_CODE),
TEMP_A AS
 (SELECT START_DATE_GMT,
         SUM(NO_OF_CALL) AS TOTAL_TRANSACTION,
         ((SUM(NO_OF_CALL)) * RATE) AS TOTAL_AMOUNT,
         ((((SUM(NO_OF_CALL)) * RATE) * TAX_RATE) / 100) AS TOTAL_VAT,
         RATE
    FROM DATA_JOIN
   WHERE COMPANY_CODE = 'RIT' 
     AND SUB_EVENT_TYPE_ID = '2'
     AND PMN_ROUTE = 'C2' 
     AND EVENT_TYPE_ID = '01'
     AND TO_CHAR(START_DATE_GMT, 'MM/YYYY') <= '01/2015'
     AND (TO_CHAR(START_DATE_GMT, 'MM/YYYY') >= '01/2015' OR START_DATE_GMT IS NULL) 
   GROUP BY START_DATE_GMT, SUB_EVENT_TYPE_ID, RATE, TAX_RATE
   ORDER BY START_DATE_GMT)

/* Result */
 SELECT 
 SUM(TOTAL_AMOUNT)AS EXCLUDE_VAT,
 SUM(TOTAL_VAT) AS SUM_VAT,
 SUM(SUM(TOTAL_AMOUNT)+SUM(TOTAL_VAT)) AS TOTAL_INCLUDE
FROM TEMP_A
GROUP BY TOTAL_AMOUNT,TOTAL_VAT;

结果 SQL 查询

EXCLUDE_VAT | SUM_VAT | TOTAL_INCLUDE
5.50        | 0.30    | 5.80

我需要结果透视图

Calculate Vat   | Total Amount
----------------------------------
EXCLUDE_VAT     | 5.50
SUM_VAT         | 0.30
TOTAL_INCLUDE   | 5.80

如何在包中枢转函数 Sum()?

【问题讨论】:

【参考方案1】:

您需要 unpivot,而不是 pivot,但 10g 本身不支持。有办法做到这一点,as shown here with decode,但在这种情况下,由于您已经拥有 CTE,您可以将三个查询联合起来:

/* Result */
SELECT 'EXCLUDE_VAT' AS CALCULATE_VAT,
  SUM(TOTAL_AMOUNT) AS TOTAL_AMOUNT
FROM TEMP_A
UNION ALL
SELECT 'TOTAL_VAT', SUM(TOTAL_VAT)
FROM TEMP_A
UNION ALL
SELECT 'TOTAL_INCLUDE', SUM(TOTAL_AMOUNT)+SUM(TOTAL_VAT)
FROM TEMP_A;

【讨论】:

【参考方案2】:

我现在无法访问 Oracle 数据库,但我记得将 PIVOT 与 WITH 语句一起使用是有限制的。我可能错了。

select * from (
    select 1 AS CV_ORDER,
       'EXCLUDE_VAT' as CALCULATE_VAT,
       SUM(TOTAL_AMOUNT) as TOTAL_AMOUNT
    from TEMP_A group by 1, 'EXCLUDE_VAT'
    union all
    select 2 as CV_ORDER,
        'SUM_VAT' as CALCULATE_VAT,
         SUM(TOTAL_VAT) as TOTAL_AMOUNT
     from TEMP_A group by 2, 'SUM_VAT'
    union all
    select 3 as CV_ORDER,
        'TOTAL_INCLUDE' as CALCULATE_VAT,
        SUM(TOTAL_AMOUNT) + SUM)(TOTAL_VAT) as TOTAL_AMOUNT
        from TEMP_A group by 3, 'TOTAL_INCLUDE'
    ) order by CV_ORDER

【讨论】:

以上是关于Oracle Sql 查询:如何在包中旋转函数 Sum()?的主要内容,如果未能解决你的问题,请参考以下文章

oracle 10g中如何缓存函数的查询结果?

我如何在包中读取函数和过程 body/ddl?

Oracle PL/SQL:如何在长包中查找未使用的变量?

PL/SQL 包 - 在包中调用过程

如何在不导入所有函数的情况下在包中使用 data.table?

R:我如何在包中添加额外的功能?