oracle VS postgresql系列-行列转换
id | name
------+---------
1001 | lottu
1001 | xuan
1001 | rax
1002 | ak
1002 | vincent
现在需要转换为
id | names
------+----------------
1001 | lottu|xuan|rax
1002 | ak|vincent
反之;oracle,postgresql有如何对待
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;
mydb=> select id,string_agg(name,'|') from lottu01 group by id;
id | string_agg
------+----------------
1002 | ak|vincent
1001 | lottu|xuan|rax
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;
--借用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系列-行列转换的更多相关文章
- oracle行列转换函数的使用
oracle 10g wmsys.wm_concat行列转换函数的使用: 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行 ...
- SQL行列转换6种方法
在进行报表开发时,很多时候会遇到行列转换操作,很对开发人员针对于SQL级别行列转换操作一直不甚理解,今天正好抽空对其进行了一些简单的总结.这里主要列举3种可以实现SQL行列转换的方法,包括通用SQL解 ...
- 数据透视表sql:用SQL行列转换实现数据透视的一些思考
用SQL行列转换实现数据透视的一些思考 摘要:根据对报表开发过程中碰到的需要用SQL行列转换进行解决的一类查询统计问题的分析,逐步探索求解得到一种较通用的解决思路,并用函数进行实现.该解决思路及函数实 ...
- Oracle学习之路-- 案例分析实现行列转换的几种方式
注:本文使用的数据库表为oracle自带scott用户下的emp,dept等表结构. 通过一个例子来说明行列转换: 需求:查询每个部门中各个职位的总工资 按我们最原始的思路可能会这么写: ...
- oracle 行列转换
oracle 行列转换列名如果是数字,用双引号包住 如下: -- 建表 create table workinfo(wid integer primary key,sid integer ,CON ...
- Oracle行列转换的思考与总结
最近几天一直在弄Oracle-SQL的问题,涉及到了一些平时没有用到的东西,也因此而在这里郁闷了好久.现在问题得到了解决虽说不算完美.但是还是和大家一起分享一下. 行列转换之一:sum(case wh ...
- 【HANA系列】SAP HANA行列转换
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA行列转换 前 ...
- 【ABAP系列】SAP ABAP 行列转换的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 行列转换的方法 ...
- Oracle行列转换
一.建表与插入数据 1.1.建表 create table kecheng ( id NUMBER, name ), course ), score NUMBER ); insert into kec ...
随机推荐
- 注意SQLServer2012中带参数的XP_ReadErrorLog
--15:26 2014-6-10数据库错误日志通知其中有一部分是检测ERRORLOG中若有用户登录失败信息时,会记录到Errorlog表,并邮件提醒.当时直接从同事那拿过来的脚本(08),按理说版本 ...
- Mac OS—苹果搭建Android开发环境
如何在MAC OS X 安装Android SDK 我以往用的开发环境都是以MAC OS安装VMware来跑不同的Windows OS,目前慢慢的将所要开发的项目由以往的传统的Wintel转移到Mob ...
- 【转】用CSS代码写出的各种形状图形的方法
一共收集整理了图形20个,比较实用,同时也为了熟悉CSS的代码.整合了一下,有错误欢迎指出. 1.正方形 #square {width: 100px;height: 100px;background: ...
- Android图片缓存的框架ImageLoader的使用
DisplayImageOptions options; // DisplayImageOptions是用于设置图片显示的类 // 使用DisplayImageOptions.Build ...
- linux:档案权限
一.例如:-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc 详细: 1.-rw-r--r--:档案类型和权限(总共十个栏位) 1.1:第一个栏位代表 ...
- paml正选择处理时序列里有终止密码子怎么处理掉
先用氨基酸序列进行比对,然后追溯回核苷酸序列,根据氨基酸序列的gap进行密码子去gap,这样不会出现终止子,能最大可能的保留其生物学意义
- CSS For Bar Graphs(maybe old)
Having a working knowledge of XHTML and CSS when developing applications is a big help in knowing wh ...
- 转:Apache POI Tutorial
Welcome to Apache POI Tutorial. Sometimes we need to read data from Microsoft Excel Files or we need ...
- Python学习总结16:时间模块datetime & time & calendar (三)
calendar模块 常见函数及说明 1 calendar.calendar(year,w=2,l=1,c=6) 返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c. 每日宽度间隔为 ...
- YbRapidSolution.MVC项目首页分页没有起作用
@model YbRapidSolution.Mvc.Models.CmsPagerDataModel <nav> <ul class="pager"> & ...