准备测试数据: 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…
1.replace()函数 语法:replace(a,b,c) a的b部分被c替换 案例:select replace(abcde,bcd,123)-- 结果a123e…
replace函数(不知支持正则表达式)语法: replace(原字段,“原字段旧内容“,“原字段新内容“,) select replace(原字段,'原字段旧内容','原字段新内容') from Table regexp_replace函数(是replace函数的扩展函数,用于通过正则表达式来进行匹配替换)例如: --http://egpweb.ybsggzyjyxxw.com/news/247/46091  替换为 http://csggzy.yibin.gov.cn:8010/Egpdzj…
http://technet.microsoft.com/zh-cn/library/ms177410.aspx unpivot : CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,    Emp3 int, Emp4 int, Emp5 int);GOINSERT INTO pvt VALUES (1,4,3,5,4,4);INSERT INTO pvt VALUES (2,4,1,5,5,5);INSERT INTO pvt VALUES…
(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…
原文: http://blog.csdn.net/jx_870915876/article/details/52403472 add by zhj: 本文是以MySQL为例说明的,但其实它适用于所有关系型数据库. 行转列的需求如下,可以发现,行转列时其实是先group by,然后将每个组中的行转为列,使用case-when + 聚合函数, 每个由行转为列的列,都要使用case-when或when-case语句.case-when是怎么作用的呢?组中的每行都经过case-when, 如果组中有N行…
本文分享在Azure Databricks中如何实现行转列和列转行. 一,行转列 在分组中,把每个分组中的某一列的数据连接在一起: collect_list:把一个分组中的列合成为数组,数据不去重,格式是['a','a','b'] collect_set:把一个分组中的列合成为集合,数据去重,格式是['a','b'] 用于连接文本数组的函数,通过sep把数组中的item分割开,连接成一个字符串: concat_ws(sep, [str | array(str)]+) 举个例子,把每个用户的gam…
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);…