行转列 && 字段拆分
explode称之为Hive爆炸函数,意思就是将一行数据炸开。
Usage:explode(array/map) explode函数传递的参数必须是一个array或者是map
hive
scala> spark.sql("select explode(split('41,52,62,35&98','[,&]')) numlist").show
+-------+
|numlist|
+-------+
| 41|
| 52|
| 62|
| 35|
| 98|
+-------+
MySQL
substring_index(str,delim,count) 说明:substring_index(被截取字段,关键字,关键字出现的次数)
表help_topic的字段help_topic_id为一个0起始的自增列,'aaa,bbb,dddd,yyy,uuu,eee'中的,被替换后,减少了5个分隔符,substring_index按,号出现的位置,截取了6次,得到的结果如下,然后使用substring_index,获取,最后一次出现的位置截取最后一个字符串分割
mysql> select help_topic_id,substring_index('aaa,bbb,dddd,yyy,uuu,eee',',',help_topic_id+1) from mysql.help_topic where help_topic_id<length('aaa,bbb,dddd,yyy,uuu,eee')+1-length(replace('aaa,bbb,dddd,yyy,uuu,eee',',','')) ;
+---------------+-----------------------------------------------------------------+
| help_topic_id | substring_index('aaa,bbb,dddd,yyy,uuu,eee',',',help_topic_id+1) |
+---------------+-----------------------------------------------------------------+
| 0 | aaa |
| 1 | aaa,bbb |
| 2 | aaa,bbb,dddd |
| 3 | aaa,bbb,dddd,yyy |
| 4 | aaa,bbb,dddd,yyy,uuu |
| 5 | aaa,bbb,dddd,yyy,uuu,eee |
+---------------+-----------------------------------------------------------------+
6 rows in set (0.00 sec)
mysql> select help_topic_id,substring_index(substring_index('aaa,bbb,dddd,yyy,uuu,eee',',',help_topic_id+1),',',-1) from mysql.help_topic where help_topic_id<length('aaa,bbb,dddd,
+---------------+-----------------------------------------------------------------------------------------+
| help_topic_id | substring_index(substring_index('aaa,bbb,dddd,yyy,uuu,eee',',',help_topic_id+1),',',-1) |
+---------------+-----------------------------------------------------------------------------------------+
| 0 | aaa |
| 1 | bbb |
| 2 | dddd |
| 3 | yyy |
| 4 | uuu |
| 5 | eee |
+---------------+-----------------------------------------------------------------------------------------+
6 rows in set (0.00 sec)
sqlserver 行转列 列转行
create table stu_score
(
name varchar(100),
math_score int,
ch_score int,
en_sore int
)
insert into stu_score
select 'tian',80,90,86
union
select 'liu',98,78,67
union
select 'wang',75,79,65
--列转行
select name,'math_score' cource,math_score
from stu_score
union
select name,'ch_score' cource,ch_score
from stu_score
union
select name,'en_sore' cource,en_sore
from stu_score

--行转列
select name,max(case cource when 'math_score' then math_score else 0 end) math_score
,max(case cource when 'ch_score' then math_score else 0 end) ch_score
,max(case cource when 'en_sore' then math_score else 0 end) en_sore from (
select name,'math_score' cource,math_score
from stu_score
union
select name,'ch_score' cource,ch_score
from stu_score
union
select name,'en_sore' cource,en_sore
from stu_score)a
group by name

行转列 && 字段拆分的更多相关文章
- kettle——入门操作-行列转换(行转列,字段拆分)
1.Row Normaliser,将一行多列数据转换为多行一列数据. 输入数据流: 计算器配置如下: 与计算器相连接的excel输出如下: Row Normaliser,设置如下, 与Row No ...
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...
- 获取dataset结果集的第一行第一列字段
DataSet fileNameDs = DbHelper.excuteSqlResultDataSet(strSql); ) { DataTable fileNameDt = fileNameDs. ...
- SQL Server 动态行转列(轉載)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段; 方法二:使用拼接SQL, ...
- SQL 行转列===列转行
行转列:sum+if 在长表的数据组织结构中,同一uid对应了多行,即每门课程一条记录,对应一组分数,而在宽表中需要将其变成同一uid下仅对应一行 在长表中,仅有一列记录了课程成绩,但在宽表中则每门课 ...
- 中等难度SQL语句(存储过程,分页,拼接字段、游标,日期类型转换,动态行转列,视图)汇总
一.创建存储过程 if Exists(select name from sysobjects where NAME = 'sp1LoginUser' and type='P')drop procedu ...
- sql 语句 获取某张表某列字段最短的某几行数据
sql 语句 获取某张表某列字段最短的某几行数据 SELECT C_name,C_code FROM Catalog where LEN(C_code)=LEN((SELECT top 1 C_cod ...
- Mysql按照字段值做分组行转列查询
今天做个后台服务,有个需求是批量生成一批表的数据,如果用BulkInsert会提升很大一截提交效率,但是如果用循环构造提交的Datable,则算法开销太高,所以用这种查询批量查出符合格式的DataTa ...
- oracle sql 字段行转列
数据库中原先如图: 现在要吧WHMM行转列: conncect by用于确定列的个数
- Oracle行转列、列转行的Sql语句总结
多行转字符串 这个比较简单,用||或concat函数可以实现 SQL Code 12 select concat(id,username) str from app_userselect i ...
随机推荐
- 敌兵布阵 HDU - 1166 - 单点修改,区间查询:树状数组/线段树
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- Java方法之命令行传递参数
命令行传参 有时候希望运行一个程序时候再传递给它消息.这要靠传递命令行参数给main()函数实现. public class Demo05 { public static void main(Stri ...
- maven工程运行环境修改 maven的java工程取mysql数据库数据
maven工程运行环境修改 <plugins> <plugin> <!-- https://mvnrepository.com/artifact/org.apache. ...
- ATM+购物车导图
1 #项目需求: 1.额度15000或者自定义 ---> 注册功能 2.实现购物商城,买东西假如购物车,调用信用卡接口结账 ---> 购物功能.支付功能 3.可以提现,手续费5% ---- ...
- 小批量excel数据导入oracle
前提是小批量数据,大批量的还是建议用sqlloader 使用PLSQL Developer工具,这个可是大名鼎鼎的Oracle DBA最常使用的工具.在单个文件不大的情况下(少于100000行),并且 ...
- iOS包大小计算
一.LinkMap文件分析 说明:LinkMap数据是根据文章<LinkMap文件分析>中方法实验实测数据. 如何获得LinkMap文件 1.在XCode中开启编译选项Write Link ...
- Java中的super和this关键字——
super与this关键字 先总结再解释: super关键字:我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类. this关键字:指向自己的引用. super注意点: 1.s ...
- 基于docker搭建Jenkins+git+python+allure
实现方式 docker+jenkins+python 执行自动化框架,生成allure报告 1.服务器上安装docker(略) 2.创建jenkins容器 3.使用Dockerfile创建有接口自动化 ...
- PyCharm 取消单词拼写检查
在使用PyCharm的时候,有时候变量不符合规范比如"old_pwd"写成了"oldpwd" ,在变量下就会出现波浪线,比较烦人 去除这个拼写检查的地方是:Fi ...
- C# core 流、字节、字符串相互转换
/// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] StreamToBytes(Stream s ...