递归PL SQL查询帮助

时间:2011-07-28 18:23:23

标签: sql oracle plsql

我有两张桌子。我想从这两个表中得到以下结果。任何帮助欣赏。


EVENT表

event_id | gross_amount | transaction_id
1        | 10           | 1
2        | 12           | 5

TRANSACTION表

trx_id | debit | credit | link_trx_id
1      | 4     | 0      | null
2      | 0     | 2      | 1
3      | 0     | 1      | 2
4      | 3     | 0      | 3
5      | 0     | 5      | null
6      | 0     | 3      | 5

结果预期:

trx_id | debit | credit | current_gross | current_net
1      | 4     | 0      | 10            | 6
2      | 0     | 2      | 6             | 8
3      | 0     | 1      | 8             | 9
4      | 3     | 0      | 9             | 6
5      | 0     | 5      | 10            | 15
6      | 0     | 3      | 15            | 18

解释

你可以看到交易1,2,3,4落入一套,而4,6落入另一套。对于每个需要的交易,它将以前的交易current_net值链接为它的current_gross。

基本上获取current_gross是一个递归调用。在这里我不能使用PL SQL函数,我可以编写一个快速递归函数来计算current_gross。我需要纯PL / SQL查询来完成这项任务。 (可以使用内置的PL SQL函数)

2 个答案:

答案 0 :(得分:2)

这是一个未经测试的近似答案(如果您为数据/结构提供了create tableinsert语句,我会对其进行测试)。它假设您的事务表中没有分支(即link_trx_id是唯一的)。基本上,我们使用递归连接来获取分组,父信息和顺序,然后使用分析函数来获得运行总计(current_net)。不是试图获得之前的总数(我们可以做到),而是从credit中删除当前行的debitcurrent_net似乎更有意义。

SELECT trx_id,
       debit,
       credit,
       root_amt - cum_debit + cum_credit + debit - credit AS current_gross,
       root_amt - cum_debit + cum_credit AS current_net
FROM   (SELECT trx_id,
               debit,
               credit,
               SUM(credit) OVER (PARTITION BY event_id ORDER BY lvl) AS cum_credit,
               SUM(debit) OVER (PARTITION BY event_id ORDER BY lvl) AS cum_debit,
               root_amt
        FROM   (SELECT     trx_id,
                           debit,
                           credit,
                           LEVEL AS lvl,
                           CONNECT_BY_ROOT (gross_amount) AS root_amt,
                           CONNECT_BY_ROOT (event_id) AS event_id
                FROM       transaction t LEFT OUTER JOIN event e ON t.trx_id = e.transaction_id
                CONNECT BY link_trx_id = PRIOR trx_id
                START WITH link_trx_id IS NULL))

答案 1 :(得分:1)

使用Allan的查询,我添加了create table和insert。 该查询的变量不匹配,所以我也纠正了这些变量(debit_cum / credit_cum与子查询中的cum-credit / cum_debit变量不匹配)。

create table event
(event_id   number(9),
 gross_amount number(9),
 transaction_id number(9));

 insert into event values (1,10,1);
 insert into event values (2,12,5);

 create table transaction
 (trx_id number(9),
  debit  number(9),
  credit number(9),
  link_trx_id number(9)
  );

  insert into transaction values (1,4,0,null);
  insert into transaction values (2,0,2,1);
  insert into transaction values (3,0,1,2);
  insert into transaction values (4,3,0,3);
  insert into transaction values (5,0,5,null);
  insert into transaction values (6,0,3,5);



SELECT trx_id,
       debit,
       credit,
       root_amt - debit_cum + credit_cum + debit - credit AS current_gross,
       root_amt - debit_cum + credit_cum AS current_net
FROM   (SELECT trx_id,
               debit,
               credit,
               SUM(credit) OVER (PARTITION BY event_id ORDER BY lvl) AS credit_cum,
               SUM(debit) OVER (PARTITION BY event_id ORDER BY lvl) AS debit_cum,
               root_amt,
               event_id
        FROM   (SELECT     trx_id,
                           debit,
                           credit,
                           LEVEL AS lvl,
                           CONNECT_BY_ROOT (gross_amount) AS root_amt,
                           CONNECT_BY_ROOT (event_id) AS event_id
                FROM       transaction t LEFT OUTER JOIN event e ON t.trx_id = e.transaction_id
                CONNECT BY link_trx_id = PRIOR trx_id
                START WITH link_trx_id IS NULL));