oracle多行轉單行多列

2021-09-01 11:50:05 字數 1836 閱讀 3852

需求:從a轉成b

需要用到以下函式:wm_concat(多行轉成一列)、case when then else end、regexp_substr

sql如下:

select material_code,

store_id,

case

when regexp_substr(wm_concat(shipping_space),'[^,]+',1,1) = '好件位' then

regexp_substr(wm_concat(place_note), '[^,]+', 1, 1)

when regexp_substr(wm_concat(shipping_space),'[^,]+',1,2) = '好件位' then

regexp_substr(wm_concat(place_note), '[^,]+', 1, 2)

when regexp_substr(wm_concat(shipping_space),

'[^,]+',1,3) = '好件位' then

regexp_substr(wm_concat(place_note), '[^,]+', 1, 3)

else

''end good,

case

when regexp_substr(wm_concat(shipping_space),'[^,]+',1,1) = '壞件位' then

regexp_substr(wm_concat(place_note), '[^,]+', 1, 1)

when regexp_substr(wm_concat(shipping_space),'[^,]+',1,2) = '壞件位' then

regexp_substr(wm_concat(place_note), '[^,]+', 1, 2)

when regexp_substr(wm_concat(shipping_space),'[^,]+',1,3) = '壞件位' then

regexp_substr(wm_concat(place_note), '[^,]+', 1, 3)

else

''end bad,

case

when regexp_substr(wm_concat(shipping_space),'[^,]+',1,1) = '待修位' then

regexp_substr(wm_concat(place_note), '[^,]+', 1, 1)

when regexp_substr(wm_concat(shipping_space),'[^,]+',1,2) = '待修位' then

regexp_substr(wm_concat(place_note), '[^,]+', 1, 2)

when regexp_substr(wm_concat(shipping_space),'[^,]+',1,3) = '待修位' then

regexp_substr(wm_concat(place_note), '[^,]+', 1, 3)

else

''end repair

from t_spt_material_place t

---若要查全部資料則去掉

where t.material_code='103010001'

group by material_code, store_id;

SQL SERVER 多行轉多列

轉換結果如上圖 建立測試標 create table 成績表 編號 int identity 1,1 not null,姓名 varchar 50 null,語文 numeric 5,2 null,數學 numeric 5,2 null,英語 numeric 5,2 null on primary ...

hive多行轉多列

一 需求 需要將多行的資料,按照某個維度轉換為一行。轉換前 轉換後 db price area name 為 東北 的total price actual db qty area name 為 東北 的qty。二 解決方案 1 sql select brand,max case when area ...

行列轉換之 多行轉多列,多列轉多行實踐版

行列轉換之 多行轉多列,多列轉多行實踐版 參考 深入行列轉換 多行轉多列,多行的計算 參考 sql server動態行列轉換 要求 實操演示 1.全表 select from temp2 2.構造row number select row number over partition by a ord...