最近,在寫儲存過程給前端返回值的時候,用到了行轉列和擷取字串的混合使用
要轉換的資料
想要實現的資料
實現sql
```sql
select old_reel,old_qty,substr(reel,0,decode(instr(reel, ','),0,length(reel),instr(reel, ',') - 1)) as newreel1,
substr(qty1,0,decode(instr(qty1, ','),0,length(qty1),instr(qty1, ',') - 1)) as reelqty1,
substr(reel,decode(instr(reel, ','),0,length(reel),instr(reel, ',') + 1)) as newreel2,
substr(qty1,decode(instr(qty1, ','),0,length(qty1),instr(qty1, ',') + 1)) as reelqty2 from (select old_reel,
old_qty,
listagg(new_reel,',') within group (order by new_reel) over (partition by old_reel) reel,
listagg(qty,',') within group (order by qty) over (partition by old_qty) qty1
from wms_reel_iqc where old_reel='551r17083000056') t where rownum=1;
##### 說明:1.
`select old_reel,
old_qty,
listagg(new_reel,',') within group (order by new_reel) over (partition by old_reel) reel,
listagg(qty,',') within group (order by qty) over (partition by old_qty) qty1
from wms_reel_iqc where old_reel='551r17083000056'` 此sql實現行轉列,用到的函式listagg
2.`select old_reel,old_qty,substr(reel,0,decode(instr(reel, ','),0,length(reel),instr(reel, ',') - 1)) as newreel1,
substr(qty1,0,decode(instr(qty1, ','),0,length(qty1),instr(qty1, ',') - 1)) as reelqty1,
substr(reel,decode(instr(reel, ','),0,length(reel),instr(reel, ',') + 1)) as newreel2,
substr(qty1,decode(instr(qty1, ','),0,length(qty1),instr(qty1, ',') + 1)) as reelqty2`此sql實現以逗號『,』擷取字串,如果要其他擷取字串,把逗號換成你想要的字元即可。
Oracle行轉列和列轉行
1.1 初始測試資料 表結構 test tb grade sql 1 create table test tb grade 2 3 id number 10 not null,4 user name varchar2 20 char 5 course varchar2 20 char 6 score...
Oracle行轉列和列轉行
一 行轉列 1.1 初始測試資料 表結構 test tb grade sql 1 create table test tb grade 2 3 id number 10 not null,4 user name varchar2 20 char 5 course varchar2 20 char 6...
Oracle行轉列和列轉行
一 行轉列 1.1 初始測試資料 表結構 test tb grade sql 1 create table test tb grade 2 3 id number 10 not null,4 user name varchar2 20 char 5 course varchar2 20 char 6...