Oracle Sql 查询:如何在包中使用 Pivot 函数 Sum()?
Oracle Sql Query : How Pivot Function Sum() in Packages?
我使用 Oracle 10g 并在包中查询。 Pivot 如何在包中运行 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
如何在包中使用 Pivot 函数 Sum()?
我现在无法访问 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
您需要取消旋转,而不是旋转,但 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;
我使用 Oracle 10g 并在包中查询。 Pivot 如何在包中运行 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
如何在包中使用 Pivot 函数 Sum()?
我现在无法访问 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
您需要取消旋转,而不是旋转,但 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;