select (select SUM(t.fees_amount)from unit u, payment p, type_of_fees t WHERE u.unit_id = p.unit_id AND p.stof = t.stof AND u.unit_id LIKE '%U1001%'
AND p.stat = 'not payed') "BLOCK 1(Not Payed)",
(select SUM(t.fees_amount)from unit u, payment p, type_of_fees t WHERE u.unit_id = p.unit_id AND p.stof = t.stof AND u.unit_id LIKE '%U1001%'
AND p.stat = 'payed') "BLOCK 1(Payed)",
(select SUM(t.fees_amount)from unit u, payment p, type_of_fees t WHERE u.unit_id = p.unit_id AND p.stof = t.stof AND u.unit_id LIKE '%U1002%'
AND p.stat = 'not payed') "BLOCK 2(Not Payed)",
(select SUM(t.fees_amount)from unit u, payment p, type_of_fees t WHERE u.unit_id = p.unit_id AND p.stof = t.stof AND u.unit_id LIKE '%U1002%'
AND p.stat = 'payed') "BLOCK 2(Payed)"
from dual;
还有其他方法可以解决这个问题吗?
答案 0 :(得分:1)
您可以使用条件聚合:
select sum(case when u.unit_id like '%U1001%' and p.stat = 'not payed' then t.fees_amount else 0 end) as "BLOCK 1(Not Payed)",
sum(case when u.unit_id like '%U1001%' and p.stat = 'payed' then t.fees_amount else 0 end) as "BLOCK 1(Payed)",
sum(case when u.unit_id like '%U1002%' and p.stat = 'not payed' then t.fees_amount else 0 end) as "BLOCK 2(Not Payed)",
sum(case when u.unit_id like '%U1002%' and p.stat = 'payed' then t.fees_amount else 0 end) as "BLOCK 2(Payed)"
from unit u
join payment p on u.unit_id = p.unit_id
join type_of_fees t on p.stof = t.stof;
此外,您应始终使用显式连接语法,而不是旧的基于逗号的连接。