【需求】例如先有数据为
 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. C#开发ActiveX插件-aspx中嵌入

    刚到新的公司,第一周让我熟悉一下他们用的silverlight和arcgis.这周,也就是昨天分配了我一个小小的任务! 哪个项目的不知道,是让我实现一个在aspx中嵌入activeX插件! 在网上找了 ...

  2. 【未解决】CImage::Save / Load 导致“线程 0xc224 已退出,返回值为 1 (0x1)”

    不知道这个返回值意味着什么,反正只要用到Save/Load就会出现这个情况. 这个链接:http://forums.codeguru.com/showthread.php?354017-The-thr ...

  3. iOS UITableView点击按钮滚到顶部

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  4. CentOS安装Git

    准备安装Gityum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel #下载git-1 ...

  5. rsync+sersync实时同步

    A: 运行rsync daemonB: 运行sersync ,会监控目录,发现改变会更新推送到A上 rsync见上面rsync设置 sersync安装配置1.建立目录mkdir -p /opt/ser ...

  6. java.util.concurrent包

    在JavaSE5中,JUC(java.util.concurrent)包出现了 在java.util.concurrent包及其子包中,有了很多好玩的新东西: 1.执行器的概念和线程池的实现.Exec ...

  7. sql server create foreign key

    in table design view(right click table and choose design), right click on a column, and select 'rela ...

  8. nyist 673 悟空的难题

    http://acm.nyist.net/JudgeOnline/problem.php?pid=673 悟空的难题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 ...

  9. php字符串首字母转换大小写的实例

    in: 后端程序首字母变大写:ucwords() <?php$foo = 'hello world!';$foo = ucwords($foo); // Hello World!$bar = ' ...

  10. UINavigationController详解二(转)页面切换和SegmentedController

    原文出自:http://blog.csdn.net/totogo2010/article/details/7682433,非常感谢. 1.RootView 跳到SecondView 首先我们需要新一个 ...