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

行转列 && 字段拆分的更多相关文章

  1. kettle——入门操作-行列转换(行转列,字段拆分)

      1.Row Normaliser,将一行多列数据转换为多行一列数据. 输入数据流: 计算器配置如下: 与计算器相连接的excel输出如下: Row Normaliser,设置如下, 与Row No ...

  2. SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...

  3. 获取dataset结果集的第一行第一列字段

    DataSet fileNameDs = DbHelper.excuteSqlResultDataSet(strSql); ) { DataTable fileNameDt = fileNameDs. ...

  4. SQL Server 动态行转列(轉載)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段; 方法二:使用拼接SQL, ...

  5. SQL 行转列===列转行

    行转列:sum+if 在长表的数据组织结构中,同一uid对应了多行,即每门课程一条记录,对应一组分数,而在宽表中需要将其变成同一uid下仅对应一行 在长表中,仅有一列记录了课程成绩,但在宽表中则每门课 ...

  6. 中等难度SQL语句(存储过程,分页,拼接字段、游标,日期类型转换,动态行转列,视图)汇总

    一.创建存储过程 if Exists(select name from sysobjects where NAME = 'sp1LoginUser' and type='P')drop procedu ...

  7. sql 语句 获取某张表某列字段最短的某几行数据

    sql 语句 获取某张表某列字段最短的某几行数据 SELECT C_name,C_code FROM Catalog where LEN(C_code)=LEN((SELECT top 1 C_cod ...

  8. Mysql按照字段值做分组行转列查询

    今天做个后台服务,有个需求是批量生成一批表的数据,如果用BulkInsert会提升很大一截提交效率,但是如果用循环构造提交的Datable,则算法开销太高,所以用这种查询批量查出符合格式的DataTa ...

  9. oracle sql 字段行转列

    数据库中原先如图: 现在要吧WHMM行转列: conncect by用于确定列的个数

  10. Oracle行转列、列转行的Sql语句总结

    多行转字符串 这个比较简单,用||或concat函数可以实现  SQL Code  12    select concat(id,username) str from app_userselect i ...

随机推荐

  1. 访问不了github解决方案

    1.首先通过网址https://github.com.ipaddress.com/www.github.com查看当前github.com对应的IP地址,查到的信息如下图所示 修改hosts文件,wi ...

  2. Path Manipulation安全漏洞处理方法

    1 public static String pathManipulation(String path) { 2 HashMap<String, String> map = new Has ...

  3. 【Java】【Mybatis】如何调用存储过程和存储函数

    https://www.jb51.net/article/230756.htm Mybatis调用存储过程 MyBatis支持使用存储过程的配置.当使用存储过程时,需要设置一个参数"mode ...

  4. rabbitmq 使用管理页面向队列中推送消息

    参考:https://blog.csdn.net/weixin_43343423/article/details/103382661

  5. pyhon_元组(tuple)

    定义: 元组中可以存储不一样类型的数据,使用小括号存储数据,中间用逗号进行分割. 元组中的数据定义好后,无法进行修改,有保护数据的目的. 格式化字符串定义多个值的时候,本质上也是元组. 测试: inf ...

  6. HTML实战:个人信息登记表

    效果展示: 代码示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  7. linux执行某个文件夹下的所有sql文件

    一个目录下有多个sql文件,hive需要执行该目录下的每个文件 #!/bin/bash dir=$(ls -l /app/test_warehouse/git/azkaban/test_shrimp/ ...

  8. tomcat 重启脚本

    1 创建脚本后将脚本放到tomcat/bin目录下并授权,就可以通过此脚本一键重启拉! #!/bin/bash tomcat_path1="$( cd "$( dirname &q ...

  9. Liunx安装eclipse-mosquitto之docker容器

    1.docker拉取 docker pull eclipse-mosquitto 2.查看镜像 docker images 3.建立配置目录 mkdir -p /mnt/mosquitto/confi ...

  10. MyBatis-Plus数据源失效找不到

    记一次项目排查问题项目应用了MyBatis-Plus多数据源配置但是在执行定时任务时发现没达到想要的结果于是查询日志分析问题最终发现问题所在多数据源注解会合事务注解冲突导致失效@DS("&q ...