我有以下数据
WITH got_grp AS
(
SELECT '1111' AS LINE_NBR, '522' AS AMOUNT_ID, 'A' AS I_N, '0' AS AMOUNT FROM dual UNION ALL
SELECT '1111' AS LINE_NBR, '121' AS AMOUNT_ID, 'A' AS I_N, '0' AS AMOUNT FROM dual UNION ALL
SELECT '1112' AS LINE_NBR, '522' AS AMOUNT_ID, 'A' AS I_N, '1' AS AMOUNT FROM dual UNION ALL
SELECT '1113' AS LINE_NBR, '122' AS AMOUNT_ID, 'A' AS I_N, '12' AS AMOUNT FROM dual UNION ALL
SELECT '1114' AS LINE_NBR, '522' AS AMOUNT_ID, 'A' AS I_N, '14.4' AS AMOUNT FROM dual UNION ALL
SELECT '1116' AS LINE_NBR, '121' AS AMOUNT_ID, 'A' AS I_N, '11' AS AMOUNT FROM dual UNION ALL
)
我想检索AMOUNT_ID的特定I_N的总计。 我希望查询返回值,如果存在所有amount_id(121,122,522)的行以及总数 金额大于30(AMOUNT_ID_121 + AMOUNT_ID_122 + AMOUNT_ID_522> 30)
例如:
I_N | AMOUNT_ID_121 | AMOUNT_ID_122 | AMOUNT_ID_522
A | 11 | 12 | 15.4
你能帮我吗?
答案 0 :(得分:0)
WITH got_grp AS
(
SELECT '1111' AS LINE_NBR, '522' AS AMOUNT_ID, 'A' AS I_N, '0' AS AMOUNT FROM dual UNION ALL
SELECT '1111' AS LINE_NBR, '121' AS AMOUNT_ID, 'A' AS I_N, '0' AS AMOUNT FROM dual UNION ALL
SELECT '1112' AS LINE_NBR, '522' AS AMOUNT_ID, 'A' AS I_N, '1' AS AMOUNT FROM dual UNION ALL
SELECT '1113' AS LINE_NBR, '122' AS AMOUNT_ID, 'A' AS I_N, '12' AS AMOUNT FROM dual UNION ALL
SELECT '1114' AS LINE_NBR, '522' AS AMOUNT_ID, 'A' AS I_N, '14,4' AS AMOUNT FROM dual UNION ALL
SELECT '1116' AS LINE_NBR, '121' AS AMOUNT_ID, 'A' AS I_N, '11' AS AMOUNT FROM dual
)
select LINE_NBR, I_N, AMOUNT_ID_522, AMOUNT_ID_121, AMOUNT_ID_122
from(
select LINE_NBR, AMOUNT_ID, I_N, AMOUNT
FROM got_grp)
PIVOT(SUM(AMOUNT) FOR AMOUNT_ID IN ('522' AS AMOUNT_ID_522, '121' AS AMOUNT_ID_121, '122' AS AMOUNT_ID_122)
)
WHERE
NVL(AMOUNT_ID_522,0)+NVL(AMOUNT_ID_121,0)+NVL(AMOUNT_ID_122,0)>30;
答案 1 :(得分:-1)
WITH got_grp AS
(
SELECT '1111' AS LINE_NBR, '522' AS AMOUNT_ID, 'A' AS I_N, '0' AS AMOUNT FROM dual UNION ALL
SELECT '1111' AS LINE_NBR, '121' AS AMOUNT_ID, 'A' AS I_N, '0' AS AMOUNT FROM dual UNION ALL
SELECT '1112' AS LINE_NBR, '522' AS AMOUNT_ID, 'A' AS I_N, '1' AS AMOUNT FROM dual UNION ALL
SELECT '1113' AS LINE_NBR, '122' AS AMOUNT_ID, 'A' AS I_N, '12' AS AMOUNT FROM dual UNION ALL
SELECT '1114' AS LINE_NBR, '522' AS AMOUNT_ID, 'A' AS I_N, '14.4' AS AMOUNT FROM dual UNION ALL
SELECT '1116' AS LINE_NBR, '121' AS AMOUNT_ID, 'A' AS I_N, '11' AS AMOUNT FROM dual
)
select *
from got_grp PIVOT(SUM(AMOUNT) FOR AMOUNT_ID IN('522' AS AMOUNT_ID_522,
'121' AS AMOUNT_ID_121,
'122' AS AMOUNT_ID_122))
WHERE AMOUNT_ID_522 IS NOT NULL
AND AMOUNT_ID_121 IS NOT NULL
AND AMOUNT_ID_122 IS NOT NULL
AND AMOUNT_ID_522 + AMOUNT_ID_121 + AMOUNT_ID_122 > 30;