帶排序的層次化查詢

 

做AppendingFee時,每張單子會產生一張對沖單,對沖金額與原單相反.

主查詢需要是按原單-原單排序,如果有對沖單的話 是原單-對沖單/原單的排序.

一開始用start with 和connect by nocycle/prior+僞列 level都沒有好的辦法解決.

 

想了個辦法  呵呵,自己搞定了,主要就是用兩個case搞定

create table test

(

col1 number,

col2 varchar2(20),

col3 number

);

 

insert into test

select 1,'ASDF','' from dual 

union all

select 2,'ASF','' from dual 

union all

select 3,'WE','2' from dual 

union all

select 4,'FGF','1' from dual 

union all

select 5,'BNM','' from dual 

union all

select 6,'678','' from dual 

union all

select 7,'CN','6' from dual 

union all

select 8,'NM','' from dual 

union all

select 9,'GGG','' from dual 

union all

select 10,'FFFF','8' from dual;

 

select * from test;

 

select col1 ap_id,col2 ap_content,

       col3 ap_hedge_id,

       orderid2 reverse_flag

from

(

select 

case when col3 is null then col1 else col3 end orderid,

case when col3 is null then 0 else 1 end orderid2,

col1,col2,col3

 from test

 ) a

 order by orderid desc,orderid2 asc;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章