oracle 行轉列 列轉行(幾種方法)

2022-06-19 13:51:17 字數 3678 閱讀 2534

工作中,我們經常會碰到行轉列的情況

這裡我介紹幾種簡單的方法--行轉列

1.oracle的pivot函式

原表

使用pivot函式:

with temp as(

select '四川省' nation ,'成都市' city,'第一' ranking from dual union all

select '四川省' nation ,'綿陽市' city,'第二' ranking from dual union all

select '四川省' nation ,'德陽市' city,'第三' ranking from dual union all

select '四川省' nation ,'宜賓市' city,'第四' ranking from dual union all

select '湖北省' nation ,'武漢市' city,'第一' ranking from dual union all

select '湖北省' nation ,'宜昌市' city,'第二' ranking from dual union all

select '湖北省' nation ,'襄陽市' city,'第三' ranking from dual

)select * from (select nation,city,ranking from temp)pivot (max(city) for ranking in ('第一' as 第一,'第二' as 第二,'第三' as 第三,'第四' as 第四));

說明:pivot(聚合函式for列名in(型別)),

其中in(『』)中可以指定別名,in中還可以指定子查詢,比如selectdistinct ranking fromtemp

select

*from

[studentscores]/*

資料來源*/asp

pivot

(

sum(score/*

行轉列後 列的值

*/) for

p.subject

/*需要行轉列的列

*/in ([

語文],[

數學],[

英語],[生物]

/*列的值*/)

) as t

2.使用max結合decode函式

原表

使用max結合decode函式

with temp as(

select '四川省' nation ,'成都市' city,'第一' ranking from dual union all

select '四川省' nation ,'綿陽市' city,'第二' ranking from dual union all

select '四川省' nation ,'德陽市' city,'第三' ranking from dual union all

select '四川省' nation ,'宜賓市' city,'第四' ranking from dual union all

select '湖北省' nation ,'武漢市' city,'第一' ranking from dual union all

select '湖北省' nation ,'宜昌市' city,'第二' ranking from dual union all

select '湖北省' nation ,'襄陽市' city,'第三' ranking from dual

)select nation,

max(decode(ranking, '第一', city, '')) as 第一,

max(decode(ranking, '第二', city, '')) as 第二,

max(decode(ranking, '第三', city, '')) as 第三,

max(decode(ranking, '第四', city, '')) as 第四

from temp group by nation;

說明:decode的用法:decode(條件,值1,返回值1,值2,返回值2,...值n,返回值n,預設值)

該函式的含義如下:

if 條件=值1 then

return

(翻譯值1)

elsif 條件

=值2 then

return

(翻譯值2)

......

elsif 條件

=值n then

return

(翻譯值n)

else

return

(預設值)

endif

3.使用max結合case when 函式

原表

使用max結合case when 函式

select case when grade_id='1' then '一年級'

when grade_id='2' then '二年級'

when grade_id='5' then '五年級' 

else null end "年級",

max(case when subject_name='語文'  then max_score

else 0 end) "語文" ,

max(case when subject_name='數學'  then max_score

else 0 end) "數學" ,

max(case when subject_name='政治'  then max_score

else 0 end) "政治"

from dim_ia_test_ysf

group by

case when grade_id='1' then '一年級'

when grade_id='2' then '二年級'

when grade_id='5' then '五年級' 

下面是介紹列轉行方法--列轉行

原表with temp as(

select '四川省' nation ,'成都市' 第一,'綿陽市' 第二,'德陽市' 第三,'宜賓市' 第四  from dual union all

select '湖北省' nation ,'武漢市' 第一,'宜昌市' 第二,'襄陽市' 第三,'' 第四   from dual

)select nation,name,title from

temp

unpivot

(name for title in (第一,第二,第三,第四))t

說明:unpivot(自定義列名/*列的值*/for自定義列名/*列名*/in(列名))

oracle行轉列 列轉行

一 行轉列 需要將如下格式 轉換為 這就是最常見的行轉列,主要原理是利用decode函式 聚集函式 sum 結合group by分組實現的 create table test id varchar2 255 primary key not null,name varchar2 255 course ...

行轉列 列轉行 oracle滴

今天做寫sql的時候遇到了列轉行的問題,以前寫過,下面是找到的方法,大家複習一下吧。列轉行 create table test name char 10 km char 10 cj int insert test values 張三 語文 80 insert test values 張三 數學 86...

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...