【需求】例如先有数据为
 id  |  name
------+---------
1001 | lottu
1001 | xuan
1001 | rax
1002 | ak
1002 | vincent
现在需要转换为
id | names
------+----------------
1001 | lottu|xuan|rax
1002 | ak|vincent
反之;oracle,postgresql有如何对待
【列转行】
oracle 
对oracle;看到这样的需求;立刻想到vm_concat,listagg函数;这样sql就出来了
select id, vm_concat(name,'|')as names from lottu01 group by id;

select id, listagg(name,'|') within group(order by null) as names from lottu01 group by id;
 postgresql
同理也有对应的函数string_agg
mydb=> select id,string_agg(name,'|') from lottu01 group by id;
id | string_agg
------+----------------
1002 | ak|vincent
1001 | lottu|xuan|rax
【行转列】
oracle
  这个一看就要使用分割函数;对oracle目前是不存在split函数
分割函数这个可以查考--http://www.cnblogs.com/lottu/p/4013751.html
当然我们可以用其他函数来替代 正则函数 regexp_substr
select id,regexp_substr(names,'[^|]+',1,level) as name from lottu02
connect by id = prior id
and prior dbms_random.value is not null
and level <= length(regexp_replace(names, '[^|]'))+1;
#或者
select id,regexp_substr(names,'[^|]+',1,level) as name from lottu02
connect by id = prior id
and prior dbms_random.value is not null
and level <= regexp_count(names, '\|')+1;
postgresql
 postgresql里面有分割函数--split_part;还是不能按照上面的写法来改;因为目前的postgresql不支持 connect by语法;
不过没关系;这并不妨碍写法不能实现。
另外 postgresql针对这个有个正则函数--regexp_split_to_table;可以直接实现。
--借用with recursive的语法来替换 connect by的写法。
mydb=> with recursive t(id,lv) as(
mydb(> select id,1 lv from lottu02
mydb(> union all
mydb(> select ts.id, t.lv+1 from t, lottu02 ts where t.id = ts.id and t.lv < length(ts.names)-length(replace(ts.names,'|','')) + 1
mydb(> )
mydb-> select t1.id,split_part(t1.names,'|',t2.lv) from lottu02 t1,t t2
mydb-> where t1.id = t2.id;
id | split_part
------+------------
1001 | lottu
1001 | xuan
1001 | rax
1002 | ak
1002 | vincent -- 借用regexp_split_to_table轻松实现。
mydb=> select id,regexp_split_to_table(names,'\|') from lottu02;
id | regexp_split_to_table
------+-----------------------
1001 | lottu
1001 | xuan
1001 | rax
1002 | ak
1002 | vincen

oracle VS postgresql系列-行列转换的更多相关文章

  1. oracle行列转换函数的使用

    oracle 10g wmsys.wm_concat行列转换函数的使用: 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行 ...

  2. SQL行列转换6种方法

    在进行报表开发时,很多时候会遇到行列转换操作,很对开发人员针对于SQL级别行列转换操作一直不甚理解,今天正好抽空对其进行了一些简单的总结.这里主要列举3种可以实现SQL行列转换的方法,包括通用SQL解 ...

  3. 数据透视表sql:用SQL行列转换实现数据透视的一些思考

    用SQL行列转换实现数据透视的一些思考 摘要:根据对报表开发过程中碰到的需要用SQL行列转换进行解决的一类查询统计问题的分析,逐步探索求解得到一种较通用的解决思路,并用函数进行实现.该解决思路及函数实 ...

  4. Oracle学习之路-- 案例分析实现行列转换的几种方式

    注:本文使用的数据库表为oracle自带scott用户下的emp,dept等表结构. 通过一个例子来说明行列转换: 需求:查询每个部门中各个职位的总工资 按我们最原始的思路可能会这么写:       ...

  5. oracle 行列转换

    oracle 行列转换列名如果是数字,用双引号包住  如下: --  建表 create table workinfo(wid integer primary key,sid integer ,CON ...

  6. Oracle行列转换的思考与总结

    最近几天一直在弄Oracle-SQL的问题,涉及到了一些平时没有用到的东西,也因此而在这里郁闷了好久.现在问题得到了解决虽说不算完美.但是还是和大家一起分享一下. 行列转换之一:sum(case wh ...

  7. 【HANA系列】SAP HANA行列转换

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA行列转换   前 ...

  8. 【ABAP系列】SAP ABAP 行列转换的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 行列转换的方法 ...

  9. Oracle行列转换

    一.建表与插入数据 1.1.建表 create table kecheng ( id NUMBER, name ), course ), score NUMBER ); insert into kec ...

随机推荐

  1. Visual Studio Online

    删除Visual Studio Online的项目http://taslimi.me/how-to-delete-a-team-project-from-tfs-online-tfs.visualst ...

  2. python+selenium实现跨浏览器兼容性测试

    python https://www.python.org/ python是一种脚本语言, 易学易用,可以助你快速实现业务逻辑,高效集成系统. ----- http://zh.wikipedia.or ...

  3. mapreduce小结

    (不断更新) MapReduce架构是一种分布式编程架构,它本质上是将任务划分,然后归并.它是以数据为中心的编程架构,相比与分布式计算和并行计算等,它更看重的是吞吐率.它处理的数据是PB级的数据,它并 ...

  4. 研究实验1_搭建一个精简的C语言开发环境(包含部分经典的前言)

    综合研究:      在这部分内容中,将启示我们如何进行独立研究和深度思考(一定要注意这一点,相应的调整自己的学习思想).同时使我们:          (1)认识到汇编语言对于深入理解其他领域知识的 ...

  5. Swift游戏实战-跑酷熊猫 08 产生源源不断的移动平台

    原理 代码实现 这节内容我们一起学习下平台的生产算法. 要点: 何时生成新的平台: 当上一个平台的右边完全进入场景的时候,就可以生成新的平台类. 如何知道上一个平台完全进入场景: 主场景中有个变量la ...

  6. Hibernate Annotation笔记

    (1)简介:在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准.它非常强大.灵活,而且具备了优异的性能.在本文中,我们将了解如何使用Java 5 注释来简化Hiberna ...

  7. Ini文件操作函数

    /// <summary> /// Copies a string into the specified section of an initialization file. /// &l ...

  8. 侧菜单栏的实现SlidingPaneLayout

    SlidingPaneLayout分为两部分,上面的 左划出部分和没划出的时候 <?xml version="1.0" encoding="utf-8"? ...

  9. java读properties的通用类,兼容linux和windows

    package util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; / ...

  10. html随笔

    <!DOCTYPE HTML> <html> <head> <meta charset = "utf-8"> <script ...