首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自不同select unions的求和值

来自不同select unions的求和值
EN

Stack Overflow用户
提问于 2020-05-21 14:54:28
回答 2查看 42关注 0票数 0

我有下一个问题:

如果你看到图像,我想要加或减值,但老实说,我不知道如何开始?

这就是我如何得到这个结果的

代码语言:javascript
复制
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
);

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

有人能帮我吗?

问候

EN

回答 2

Stack Overflow用户

发布于 2020-05-21 15:05:56

因为它是关于Oracle Application Express的,所以我建议您让Apex来做这项工作;它完全有能力做到这一点。多么?只需创建一个报告(经典的或交互式的,无关紧要)。

重要的是,你应该--以某种方式--知道哪些值是正的,哪些是负的。

如果您将它们存储在一个表中,效果会更好。如果不是,则使用CASE,这样屏幕上的值就会真正显示出来(正/负)。然后:

  • 对于交互式报表,请转到"Actions“菜单,选择"Data”和"Aggregate";对于经典报表,请使用value column
  • 上的"Sum“函数,转到value列的属性并选中"Compute sum”属性

很简单,不是吗?

如果你选择手动做所有的事情,那么,为了得到一个有问题的结果,这将是大量的编码/努力。你知道Apex在主页上写了什么吗?

构建企业应用的速度提高了20倍,代码减少到原来的1/100

我建议你这么做。

票数 0
EN

Stack Overflow用户

发布于 2020-05-21 20:27:48

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61928928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档