【问题标题】:Re-use a calculated field value in the same row - SQL重复使用同一行中的计算字段值 - SQL
【发布时间】:2014-04-28 14:48:28
【问题描述】:

我想要做的是编写一个查询,提供以下输出:

帐号的日期值是一个计算字段,它使用当前数据库中的信息计算一个数字。然后对于第 +1 天,我需要从日期值开始计算。但是我怎样才能传递这个值,以便我可以在第 +1 天使用它来计算呢?我怎样才能做到这一点?他们不喜欢在我的工作场所使用 pl sql,所以这是最坏的情况。

目前我只写了一天的查询来获取所有 16 个帐户的信息:

 select OBforeachaccount.accountnumber accountnumbers, (OBforeachaccount.OB - NVL(IDforeachaccount.sumamounts,0) - NVL(paymentsforeachaccount.sumpayments,0)) as day1, day1+5 as day2
    from
    --ophalen van CB day -1
    (select account_id accountnumber,cal_date,cb_amount_default OB from fillinggaps g, dim_date d
    where g.balance_date = d.cal_date and
    d.cal_date = TO_CHAR(sysdate -1,'DD/MM/YYYY')
    order by account_id, cal_date) OBforeachaccount,

    (select a.id accountnumber, cal_date, sum(AMOUNT_USD) sumamounts
    from fact_id_transaction i, dim_date d, dim_account a
    where i.value_date_id = d.id and 
    i.account_id  = a.id and 
    d.cal_date = TO_CHAR(sysdate -1,'DD/MM/YYYY')
    --future: add filter based on the account number
    group by a.id, cal_date
    order by a.id,cal_date) IDforeachaccount,

    (select ordering_account_id accountnumber,cal_date, sum(instructed_amount_default) sumpayments
    from fact_payment p, dim_date d
    where 
    p.value_date_id = d.id and
    d.cal_date = TO_CHAR(sysdate -1,'DD/MM/YYYY')
    group by ordering_account_id, cal_date
    order by ordering_account_id, cal_date) paymentsforeachaccount
    where 
    OBforeachaccount.accountnumber = IDforeachaccount.accountnumber(+) and
    OBforeachaccount.accountnumber = paymentsforeachaccount.accountnumber(+)

谢谢大家的建议

【问题讨论】:

  • 您使用的是哪个版本的 Oracle? 11gR2?
  • 我使用的是 11G 版本

标签: sql oracle subquery oracle-sqldeveloper correlated-subquery


【解决方案1】:

我建议使用内联视图来获取帐号和第一天的值,然后在包含查询中根据需要在其他列中引用第一天的值。

在伪查询中:

select
    account_number,
    day1,
    day1+x day2,
    day1+y day3
from
    (select
        account_number,
        intensive_calculation day1
    from
        tables
    where
        something = something);

【讨论】:

  • 我目前正在尝试使用内联视图执行此操作。它有效,但它给出了一些严重的性能问题。我的查询的加载时间为 8 秒。感谢您的回答!
  • 内联视图应该不会真正影响性能,Oracle 优化器会很好地处理它。您对内联视图有任何联接吗?我会看看内部查询的性能,检查解释计划。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
相关资源
最近更新 更多