行转列 && 字段拆分
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 ...
随机推荐
- 5. icon创建
1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="U ...
- git stash (pycharm/vscode的gui演示)
git stash (pycharm/vscode的gui演示) 场景 代码刚写到一半,发现线上bug需要马上修改部署上线 此时手头的代码写一半,提交根本跑不动甚至影响原来的业务了 回滚就白瞎搬了好几 ...
- Dashboard是什么意思 Dashboard怎么用?
Dashboard是什么意思?Dashboard怎么用?可能很多Mac用户朋友都不知道,Dashboard是苹果OS X操作系统的一大特色.是随苹果公司 Mac OS X 10.4 Tiger 操作系 ...
- 前端将JSON数据格式化显示
很简单 1 formatJsonData(jsonData) { 2 var smapleDetailData = JSON.stringify(JSON.parse(jsonData), null, ...
- Python面向对象编程——一些类定义(杂)
一.abstractmethod 子类必须全部实现重写父类的abstractmethod方法 非abstractmethod方法可以不实现重写 带abstractmethod方法的类不能实例化 fro ...
- monogo-shell
创建集合 use person //创建数据库或进入数据库 db.createCollection(table_name) 主键 插入数据时会自动生成主键,保证每条数据唯一性 生成对象主键 > ...
- HTML实战:个人信息登记表
效果展示: 代码示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- 再见IE
- git 报错 incorrect username or password
如果报这个 就是用户名 或密码错误 添加个正确的凭据就好了
- quartus报错 Error (10054): Verilog HDL File I/O error at sdram_ctrl_tb.v(6): can't open Verilog Design File "Sdram_params.h"
解决方法:包含完整路径. 比如我一开始是:`include "Sdram_params.h" 错误(改为:`include "F:\FPGA\exce\uart2sdra ...