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 from temp

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

Oracle行转列/列转行的更多相关文章

  1. oracle 行转列、列转行

    最近做数据处理,经常遇到需要行转列.列转行的场景,记录个非常简单实用的oracle  列转行.行转的列方法 1.行转列,基础数据如下 做行转列处理 处理SQL select user_name,max ...

  2. oracle行转列、列转行、连续日期数字实现方式及mybatis下实现方式

    转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9977591.html 九月份复习,十月份考试,十月底一直没法收心,赶在十一初 由于不可抗拒的原因又不得不重新找 ...

  3. Oracle 行转列pivot 、列转行unpivot 的Sql语句总结

    这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...

  4. Oracle 行转列及列转行

    参考网址:http://blog.163.com/fushahui_1988@126/blog/static/82879994201192844355174/ 一.多行转一列select id, vn ...

  5. Oracle 多行变一列的方法

    多行变一列的方法有很多,觉得这个第一眼看懂了当时就用的这个办法. 情况是这样的.以下数据前几列是一样的,需要把VAT_VALUE_CHAR 的值放在同一行上. SELECT * FROM ps_vat ...

  6. oracle 多行转多列查询

     oracle 多行转多列查询  ---create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int);inse ...

  7. oracle 行转列 分析函数

    oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...

  8. Oracle 多行转多列

    Oracle 多行转多列,列值转为列名   前段时间做调查问卷,客户创建自定义问卷内容,包括题目和选项内容; 之后需要到处问卷明细,,,,麻烦来咯 于是到网上到处搜索,没有直接结果;于是又找各种相似的 ...

  9. SQL Server 行转列,列转行。多行转成一列

    一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + va ...

  10. Oracle字符串行拆分成列的三种方式

    Oracle字符串行拆分成列的三种方式 --muphy 开发过程中经常会遇到将前台多个值用逗号连接一同传递到后台查询,这个用逗号连接的字符串分隔的每个字符串分别对应Oracle数据库表的不同行. 如下 ...

随机推荐

  1. SQL SERVER学习笔记:临时表与表变量

    本文主要摘自徐海蔚的<Microsoft SQL SERVER企业级平台管理实践> 表变量可以作为存储过程的返回参数,而临时表不行.(存疑?表值参数只在SQL SERVER2008才开始支 ...

  2. ubuntu16.04安装flash player

    ubuntu16.04安装flash player sudo apt-get install flashplugin-installer 1 2019: apt-get install browser ...

  3. 修改select默认样式

    http://www.qkzone.com/code/2015-11-26/1.html

  4. hdoj--2073--无限的路(数学规律)

     无限的路 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. algorithm库———count&&countif

    algorithm头文件定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果. 编写程序读取一系列int型数据,并将它们存储到vecto ...

  6. 洛谷 P3806 点分治模板

    题目:https://www.luogu.org/problemnew/show/P3806 就是点分治~ 每次暴力枚举询问即可,复杂度是 nmlogn: 注意 tmp[0]=1 ! 代码如下: #i ...

  7. 60.extjs-布局 (在column布局中使用fieldset 和 在fieldset中使用column布局)

    转自:https://blog.csdn.net/snn1410/article/details/8817821/ 在标准的html中,需要把输入项都放到fieldset中,一次来显示分组结构.虽然E ...

  8. 49.Ext.form.TextField()基本用法

    转自:https://blog.csdn.net/toudoulin/article/details/6719163 var textfieldName = new Ext.form.TextFiel ...

  9. js中的slice()、substring()、substr()、split()、join()、indexof()

    在js中字符截取函数有常用的三个slice().substring().substr()了,下面我来给大家介绍slice().substring().substr()函数在字符截取时的一些用法与区别吧 ...

  10. Linex系统 配置php服务器

    此文是可以参考 楼主也不是系统管理员只是迫不得已所以自己才找的  大家可以参考 .... ..... 安装apache 安装mysql 安装PHP 测试服务器 php -v 查询php的版本 就这些了 ...