我想在选择表演时一步到桌子。
我可以在一个查询中计算COMPLETED_AMOUNT
和PENDING_AMOUNT
吗?
我简化了查询,因此无法添加主查询,性能降低
SELECT D.COLUMN5,
D.COLUMN2,
(SELECT SUM(J.AMOUNT)
FROM EFT_TABLE J
WHERE J.COLUMN1 = D.COLUMN1
AND J.COLUMN2 = D.COLUMN2
AND J.COLUMN3 = D.COLUMN3
AND J.COLUMN4 = 'N'
AND J.COLUMN5 = D.COLUMN5
AND J.COLUMN6 = 'CL'
AND J.COLUMN7 ='B' )AS COMPLETED_AMOUNT,
(SELECT SUM(J.AMOUNT)
FROM EFT_TABLE J
WHERE J.COLUMN1 = D.COLUMN1
AND J.COLUMN2 = D.COLUMN2
AND J.COLUMN4 = 'N'
AND J.COLUMN5 = D.COLUMN5
AND J.COLUMN3 = D.COLUMN3
AND J.COLUMN6 = 'WC'
AND J.COLUMN7 = ('B') )AS PENDING_AMOUNT
FROM EFT_TABLE D
WHERE D.COLUMN7 = 'A'
AND D.COLUMN3 >= trunc(sysdate-1)
AND D.COLUMN3 <= trunc(sysdate)
答案 0 :(得分:0)
您可以根据COLUMN6离开连接表和总金额。试试这个。 但这取决于COLUMN5和COLUMN2的独特性。 没有足够的数据来准确回答。
SELECT D.COLUMN5, D.COLUMN2,
SUM(DECODE(J.COLUMN6, 'CL', J.AMOUNT, 0)) COMPLETED_AMOUNT,
SUM(DECODE(J.COLUMN6, 'WC', J.AMOUNT, 0)) PENDING_AMOUNT
FROM EFT_TABLE D
LEFT JOIN EFT_TABLE J ON (D.COLUMN1 = J.COLUMN1 AND
D.COLUMN2 = J.COLUMN2 AND
D.COLUMN3 = J.COLUMN3 AND
J.COLUMN4 = 'N' AND
D.COLUMN5 = J.COLUMN5 AND
J.COLUMN7 = 'B')
WHERE D.COLUMN7 = 'A'
AND D.COLUMN3 >= trunc(sysdate-1)
AND D.COLUMN3 <= trunc(sysdate)
GROUP BY D.COLUMN5, D.COLUMN2
答案 1 :(得分:0)
有条件地聚合(即在CASE WHEN
内使用SUM
):
select
d.column5,
d.column2,
j.completed,
j.pending
from eft_table d
left join
(
select
column1, column2, column3, column5,
sum(case when column6 = 'CL' then amount end) as completed,
sum(case when column6 = 'WC' then amount end) as pending
from eft_table
where column4 = 'N'
and column7 = 'B'
group by column1, column2, column3, column5
) j on j.column1 = d.column1
and j.column2 = d.column2
and j.column3 = d.column3
and j.column5 = d.column5
where d.column7 = 'A'
and d.column3 between trunc(sysdate) - 1 and trunc(sysdate)
order by d.column5, d.column2;