【问题标题】:Sum values from different select unions来自不同选择联合的总和值
【发布时间】:2020-05-21 06:54:28
【问题描述】:

我有下一个问题:

如果您看到图片,我想要添加或减去 VALUES,但老实说,我不知道如何开始?

这就是我得到这个结果的方式

select position, position_1, TIPO_RESUMEN, CONCEPT, to_char(NVL(VALUE,0) ,'FM999G999G999G999G990D00', 'NLS_NUMERIC_CHARACTERS='',.''') as VALUE
from (
    SELECT 1 position, 10 position_1, 'Expense' TIPO_RESUMEN, 'Expected expense' CONCEPT ,sum(IMP_GA_GU) VALUE from table_1 tg where UPPER(tg.cod) = 'USER_1' and tg.cod_fol = :P10_FOL and tg.prod_cuad = :P10_PROD and tg.fecha_inicio = :P10_FEC_INI and tip_ga = 'PREVISTO'
    union
    SELECT 2 position, 20 position_2, 'Expense' TIPO_RESUMEN, 'Unforeseen expense' CONCEPT ,sum(IMP_GA_GU) VALUE from table_2 tp where UPPER(tp.cod) = 'USER_1' and tp.cod_fol = :P10_FOL and tp.prod_cuad = :P10_PROD and tp.fecha_inicio = :P10_FEC_INI and tp.tip_ga = 'NO_PREVISTO'
    union
    SELECT 3 position, 30 position_3, 'Expense' TIPO_RESUMEN, 'Total' CONCEPT , 0 VALUE from dual
);

我需要总价值是(预期费用 + 意外费用)的总和

谁能帮帮我?

问候

【问题讨论】:

    标签: sql oracle select union


    【解决方案1】:

    因为它是关于 Oracle Application Express,我建议您让 Apex 来完成这项工作;它完全有能力做到这一点。如何?只需创建一个报告(经典或交互式,没关系)。

    重要的是你应该——不知何故——知道哪些是积极的,哪些是消极的。

    如果您将它们这样存储到表中,那就更好了。如果不是,请使用CASE,以便屏幕上的值真正显示为这样(正/负)。那么:

    • 对于交互式报告,进入“操作”菜单,选择“数据”和“聚合”;在value 列上使用“求和”函数
    • 对于经典报告,请转到value 列的属性并检查“计算总和”属性

    很简单,不是吗?

    如果您选择手动执行所有操作,那么,对于一个有问题的结果将需要大量的编码/努力。你知道 Apex 在主页上说什么吗?

    将企业应用程序的构建速度提高 20 倍,代码减少 100 倍

    我建议你这样做。

    【讨论】:

    • 这部分是某人做的一个项目,这部分是这样完成的,我可以修改,因为可以花更多的时间(我没有的东西)。所以有一个选项可以通过查询来做到这一点?问候
    • 我想。使用 CASE(或 DECODE)区分正负值并将 SUM 应用于这些值。
    【解决方案2】:

    您可以使用 CTE,然后使用 union all

    with cte as (
          select 1 as position, 10 as position_1, 'Expense' as TIPO_RESUMEN, 'Expected expense' as CONCEPT , sum(IMP_GA_GU) as VALUE
          from table_1 tg
          where UPPER(tg.cod) = 'USER_1' and tg.cod_fol = :P10_FOL and tg.prod_cuad = :P10_PROD and tg.fecha_inicio = :P10_FEC_INI and tip_ga = 'PREVISTO'
          union all
          select 2 as position, 20 as position_2, 'Expense' as TIPO_RESUMEN, 'Unforeseen expense' as CONCEPT, sum(IMP_GA_GU) as VALUE
          from table_2 tp 
          where UPPER(tp.cod) = 'USER_1' and tp.cod_fol = :P10_FOL and tp.prod_cuad = :P10_PROD and tp.fecha_inicio = :P10_FEC_INI and tp.tip_ga = 'NO_PREVISTO'
         )
    select cte.*
    from cte
    union all
    select 3 as position, 30 as position_3, 'Expense' as TIPO_RESUMEN, 'Total' as CONCEPT , sum(value)
    from cte
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      相关资源
      最近更新 更多