行转列 && 字段拆分
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 ...
随机推荐
- 哈希表相关题目-python
栈&队列&哈希表&堆-python https://blog.csdn.net/qq_19446965/article/details/102982047 1.O(1)时间插 ...
- Expected indentation of 2 spaces but found 4
预期缩进2个空格,但发现4个 把缩进空格修改后如图
- Expected space(s) after "default" keyword-spacing
添加空格
- Java基础之类型转换
类型转换 由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换 低 --------------------------------------------------------&g ...
- 关于el-dialog弹窗组件关闭报错事件
以下写法,向父组件抛出关闭事件, (正常点击弹窗footer的关闭时没有报错,但是点击空白处及右上角的×号,就会报以上错误) 原因, close事件为已经关闭了弹窗后的事件,官方还给出了 befor ...
- threadlocal应用
public class DataSourceSelector { /** * 线程threadlocal */ private static ThreadLocal<String> db ...
- 【linux】grep命令检索大批量日志中的堆栈日志
记得3年前,我为了查看100M日志文件里面的错误堆栈信息,百度了许久都毫无结果 没想到今天再次百度时,一下子看到了grep -A 命令,激动不已. 原来只需要用, grep -A 100 'KeyWo ...
- idea提交时忽略.class、.iml文件和文件夹或目录的方法
第一种方法 在Setings–> Editor --> File Types -->Ignore files and folders中添加需要忽略的文件和文件夹: .idea 忽略 ...
- 工作三年的.NET程序员现状及其感悟
算上实习,已经工作三年了.时间过的真的很快,我也从一开始的非标自动化行业成功转入了医疗器械行业,如今在苏州园区的BioBay工作,这里我每天都工作的挺开心的.也于11.6号第一次和如今的女朋友见面,并 ...
- windows下解决mysql5中文乱码的问题
1.问题描述:一开始无论是在命令行,还是在mysql的客户端输入中文都会出现 "???" 问题之类的乱码问题: 2.解决办法: 1)cmd 进入mysql ,命令mysql -ur ...