【问题标题】:oracle sql plus script is too longoracle sql plus脚本太长
【发布时间】:2017-04-15 01:10:47
【问题描述】:
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;

还有其他方法可以解决吗?

【问题讨论】:

    标签: oracle sqlplus


    【解决方案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;
    

    此外,您应该始终使用显式连接语法,而不是旧的基于逗号的连接。

    【讨论】:

    • 是不是因为我的数据库设计不好,导致脚本这么长??
    • @user7868404 - 我认为这是要求。由于您必须使用like,因此查询可能会很慢。虽然我已经把你的四个选择合二为一了。
    猜你喜欢
    • 1970-01-01
    • 2021-10-25
    • 2012-11-13
    • 2019-06-06
    • 2010-12-16
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2011-10-15
    相关资源
    最近更新 更多