我有一个表名ID_TXN,其中" RECONCILE_SL"列有多个txn,其中1txn有多个反对txn.I想要显示它们如下......
My Table Data
| RECONCILE_SL | AMOUNT_DR | AMOUNT_CR | TR_REF_NO | TRN_DATE |
| ------------ | --------- | --------- | ----------- | ----------|
| 280874 | 0 | 200000 | 1703700164 | 07-Feb-17 |
| 280874 | 0 | 200000 | 1703900110 | 09-Feb-17 |
| 280874 | 0 | 200000 | 1704400070 | 14-Feb-17 |
| 280874 | 200000 | 0 | AIBIF170137 | 07-Feb-17 |
| 280874 | 400000 | 0 | AIBIF170149 | 09-Feb-17 |
My Desire Result
| TRN_DATE | TR_REF_NO | AMOUNT_DR | AMOUNT_CR | ContraTRN_DATE | ContraTR_REF_NO | Contra AMOUNT_DR | Contra AMOUNT_CR | RECONCILE_SL |
|-----------|-------------|-----------|-----------|----------------|-----------------|------------------|------------------|--------------|
| 07-Feb-17 | AIBIF170137 | 200000 | | 07-Feb-17 | 1703700164 | | 200000 | 280874 |
| 09-Feb-17 | AIBIF170149 | 400000 | | 09-Feb-17 | 1703900110 | | 200000 | 280874 |
| | | | | 14-Feb-17 | 1704400070 | | 200000 | 280874 |
答案 0 :(得分:1)
使用函数rn
添加人工序列(row_number()
)并自行加入您的数据,例如:
select reconcile_sl,
dr.trn_date date_dr, dr.amount_dr, dr.tr_ref_no ref_no_dr,
cr.trn_date date_cr, cr.amount_cr, cr.tr_ref_no ref_no_cr
from (
select t.*, row_number() over (partition by reconcile_sl order by tr_ref_no) rn
from id_txn t where amount_dr > 0) dr
full join (
select t.*, row_number() over (partition by reconcile_sl order by tr_ref_no) rn
from id_txn t where amount_cr > 0) cr
using (reconcile_sl, rn)
测试:
create table id_txn (RECONCILE_SL number(8), AMOUNT_DR number(8), AMOUNT_CR number(8),
TR_REF_NO varchar2(15), TRN_DATE date);
insert into id_txn values(280874, 0, 200000, '1703700164', date '2017-02-07');
insert into id_txn values(280874, 0, 200000, '1703900110', date '2017-02-09');
insert into id_txn values(280874, 0, 200000, '1704400070', date '2017-02-14');
insert into id_txn values(280874, 200000, 0, 'AIBIF170137', date '2017-02-07');
insert into id_txn values(280874, 400000, 0, 'AIBIF170149', date '2017-02-09');
结果:
RECONCILE_SL DATE_DR AMOUNT_DR REF_NO_DR DATE_CR AMOUNT_CR REF_NO_CR
------------ ----------- --------- --------------- ----------- --------- ---------------
280874 2017-02-07 200000 AIBIF170137 2017-02-07 200000 1703700164
280874 2017-02-09 400000 AIBIF170149 2017-02-09 200000 1703900110
280874 2017-02-14 200000 1704400070
答案 1 :(得分:0)
我写了我的解决方案(查询)
SELECT *
FROM (SELECT acc_no
,dr.trn_date ledger_trn_date
,dr.tr_ref_no tr_ref_no
,dr.amount_dr
,dr.amount_cr
,cr.trn_date contra_trn_date
,cr.tr_ref_no contra_tr_ref_no
,cr.amount_dr contra_amount_dr
,cr.amount_cr contra_amount_cr
,rec_category
,reconcile_sl
,process_user
,type_of_recons
FROM (SELECT t.*, ROW_NUMBER () OVER ( PARTITION BY reconcile_sl ORDER BY tr_ref_no) rn
FROM ID_TXN t
WHERE acc_type = 'S') dr
FULL JOIN (SELECT t.*, ROW_NUMBER () OVER ( PARTITION BY reconcile_sl ORDER BY tr_ref_no) rn
FROM ID_TXN t
WHERE acc_type = 'N') cr
USING (reconcile_sl
,acc_no
,rec_category
,process_user
,type_of_recons
,rn)) m
WHERE acc_no = '101974'
AND reconcile_sl = 280874
AND EXISTS
(SELECT 1
FROM ID_TXN h
WHERE trn_date BETWEEN :day_1 AND :day_2
AND m.reconcile_sl = h.reconcile_sl)
ORDER BY reconcile_sl, ledger_trn_date, contra_trn_date
Result:
reconcile_sl ledger_trn_date ledger_tr_ref_no amount_dr amount_cr contra_trn_date contra_tr_ref_no contra_amount_dr contra_amount_dr rec_category process_user type_of_recons
------------ --------------- ---------------- --------- ---------- --------------- ---------------- ---------------- ---------------- ------------ ------------ --------------
280874 07/feb/17 aibif170137 200000 0 07/feb/17 1703700164 0 200000 s-n saleh m
280874 09/feb/17 aibif170149 400000 0 09/feb/17 1703900110 0 200000 s-n saleh m
280874 14/feb/17 1704400070 0 200000 s-n saleh m