oracle 行转列~列转行的几种方法

这里我介绍几种简单的方法--行转列

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中还可以指定子查询,比如 select distinct ranking fromtemp

复制代码

SELECT * FROM [StudentScores] /*数据源*/
AS P
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(缺省值)
END IF

复制代码

 

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 '五年级' 

else null end

 

 

 

下面是介绍列转行方法--列转行

原表

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(列名))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值