Oracle中行列轉換方法

2021-05-22 17:45:54 字數 1368 閱讀 7692

實際查詢中,可能會出現將某些查詢結果值顯示在列位置上,此時可之直接通過sql語句進行處理:

1. 方法-:使用sql的函式轉換:

select aa.po_no,aa.po_line,

nvl((case when aa.group_name='re' then aa.qty end),0) as re,

nvl((case when aa.group_name='sh' then aa.qty end),0) as sh,

nvl((case when aa.group_name='pa' then aa.qty end),0) as pa,

nvl((case when aa.group_name='we' then aa.qty end),0)as we

from  (select s.po, s.line, s.group_name, count(*) as qty

from table1 s, table2 v1

where   v.po = s.po and v.po = s.line and s.group_name in ('re', 'sh', 'pa', 'we')

and v1.date between to_date('20100605', 'yyyymmdd') and

to_date('20100607', 'yyyymmdd'))aa

2. 方法=:直接通過串表得到:

select v.po,v.line,

(select count(*)

from table1 s

where v.po = s.po and v.po = s.line

and s.group_name ='sh') as sh,

(select count(*)

from table1 s

where v.po = s.po and v.po = s.line

and s.group_name ='re') as re,

(select count(*)

from table1 s

where v.po = s.po and v.po = s.line

and s.group_name ='pa') as pa, 

(select count(*)

from table1 s

where v.po = s.po and v.po = s.line

and s.group_name ='we') as we,                   

from table2 v

where v.date between to_date('20100605', 'yyyymmdd') and to_date('20100607', 'yyyymmdd')

Hive中行列轉換

1 演示多列轉為單行 資料檔案及內容 student.txt xiaoming english 92.0 xiaoming chinese 98.0 xiaoming math 89.5 huahua chinese 80.0 huahua math 89.5 建立表studnet create t...

mysql中行列表示 mysql 行列轉換怎麼寫?

一段sqlserversql 完成行列轉換,但在mysql裡無法執行,請問mysql的應該怎麼寫?createtablea1 no1int,no2varchar 10 mya1int,mya2int,mya3int,mya4int,mya5int,mya6int inserta1.一段sqlserv...

Oracle行列轉換

1.列轉行 create table t col row id int,c1 varchar2 10 c2 varchar2 10 c3 varchar2 10 insert into t col row values 1,v11 v21 v31 insert into t col row valu...