Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从输入日期起一周开始查找数据,并显示为列_Sql_Oracle - Fatal编程技术网

Sql 从输入日期起一周开始查找数据,并显示为列

Sql 从输入日期起一周开始查找数据,并显示为列,sql,oracle,Sql,Oracle,对于我正在制作的一份报告,我有以下问题: select gam.bank_id, gam.sol_id, gam.acct_crncy_code Account_Currency, gam.gl_sub_head_code GLSH_Code, gam.foracid, (select tran_date_bal from tbaadm.eab where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab w

对于我正在制作的一份报告,我有以下问题:

select
gam.bank_id,
gam.sol_id,
gam.acct_crncy_code Account_Currency,
gam.gl_sub_head_code GLSH_Code,
gam.foracid,
(select tran_date_bal from tbaadm.eab
    where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
        where gam.acid = eab.acid
            and eab.eod_date <= '3-Jun-2013')
    and gam.acid = eab.acid
    and gam.bank_id = eab.bank_id) balance_1,
它根据输入的日终日期获取帐户的日终余额。我接下来需要做的是还显示该账户一整周的EOD余额。在这种情况下,由于2013年6月3日是我的输入日期,我还需要显示该账户6月2日至6月1日的EOD余额。。。直到5月28日。我还需要将它们显示为结果集中的列。到目前为止,我所做的是:

(select tran_date_bal from tbaadm.eab
    where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
        where gam.acid = eab.acid
            and eab.eod_date <= to_date('3-Jun-2013') - 1)
    and gam.acid = eab.acid
    and gam.bank_id = eab.bank_id) balance_2,
(select tran_date_bal from tbaadm.eab
    where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
        where gam.acid = eab.acid
            and eab.eod_date <= to_date('3-Jun-2013') - 2)
    and gam.acid = eab.acid
    and gam.bank_id = eab.bank_id) balance_3
.
.
.
(select tran_date_bal from tbaadm.eab
    where eab.eod_date = (select max(eab.eod_date) from tbaadm.eab
        where gam.acid = eab.acid
            and eab.eod_date <= to_date('3-Jun-2013') - 6)
    and gam.acid = eab.acid
    and gam.bank_id = eab.bank_id) balance_7
正如你所看到的,是的,我几乎把这该死的东西硬编码了。问题是,随着我越来越清楚地说明报告的规格,我需要添加更多的列,这些列还可以查看日期为2013年6月3日是星期一,6月2日是星期天。。。5月28日是星期二,也是这些日期的相应汇率。当然,以这种速度,硬编码这些东西只会使查询过于臃肿,将来可能还有其他数据,我必须从输入日期前整整一周开始提取


我现在的问题是,在Oracle中是否有另一种更有效的方法来执行此操作,即查询查找从一周开始到给定日期的数据,并且数据将在查询中显示为列。

不幸的是,没有,我们在我公司的报告中也做了同样的事情,因为您的子查询使用gam.acid=eab.acid,并且您的子查询中需要gam.acid。我假设gam是一个驱动表或查询中的一个主表,那么您必须为每个gam.acid运行此迭代

有时我们以这种方式编写报告,有时我们对每个条件使用左连接,而不是在select语句中使用子查询,这取决于开发人员的首选项,但两者都返回相同的内容和相同的性能