准备测试数据: create table test01( groupid number, a number, b number, c number ); insert into test01 values(1,2,3,4); insert into test01 values(1,2,6,4); insert into test01 values(1,7,3,9); insert into test01 values(2…
(case '字段' when '数据1' then '输出1' when '数据2' then '输出2' when '数据3' then '输出3' else '其他数据输出一致' end) as '字段' replace(字段,'原子字符串','新子字符串') as 字段 select username, (case `role` when 0 then '班长' when 1 then '课代表' when 2 then '组长' when 3 then '学生' else '超级管理员…
首先pivot是一个列转行的函数,反向用是unpivot(行转列). 在SQL sever中可以这么写 SELECT * FROM [TABLE] /*数据源*/ AS A PIVOT ( MAX/*聚合函数*/(COL1/*行转列后 列的值*/) FOR A.COL2/*需要行转列的列*/ IN ([VALUE1],[VALUE2],[VALUE3].../*列的值*/) ) AS B 在Oracle中可以这么写 select * from tab pivot(max /*聚合函数*/ (co…
test表: 执行列转行sql: select student, sum(case Course when '语文' then Score else null end) 语文, sum(case Course when '数学' then Score else null end) 数学, sum(case Course when '英语' then Score else null end) 英语 from test group by student 得到结果:…
一.行转列 需要将如下格式 转换为: 这就是最常见的行转列,主要原理是利用decode函数.聚集函数(sum),结合group by分组实现的 create table test( id varchar2(255) primary key not null, name varchar2(255), course varchar2(255), score varchar2(255) ); insert into test values(sys_guid(),'zhangsan','语文',85);…