表:
col1 col2 col3
A 1.5 a
A 1.5 b
A 2.5 c
A 2.5 d
B 5.5 e
B 5.5 f
B 1.2 g
C 1.2 h
A 1.2 i
A 1.1 j 我想取得这样的结果:
A 1.5 a,b
A 2.5 c,d
A 1.1 J
A 1.2 i
B 5.5 e,f
C 1.2 h 也就是按col1、col2分组统计,将col3的值用“,”连接起来。 其中col1 是 字符型 col2是数字型。这两个字段的唯一值的个数是未知的 不知该如何才能做到,请各位高手帮忙。
col1 col2 col3
A 1.5 a
A 1.5 b
A 2.5 c
A 2.5 d
B 5.5 e
B 5.5 f
B 1.2 g
C 1.2 h
A 1.2 i
A 1.1 j 我想取得这样的结果:
A 1.5 a,b
A 2.5 c,d
A 1.1 J
A 1.2 i
B 5.5 e,f
C 1.2 h 也就是按col1、col2分组统计,将col3的值用“,”连接起来。 其中col1 是 字符型 col2是数字型。这两个字段的唯一值的个数是未知的 不知该如何才能做到,请各位高手帮忙。
解决方案 »
- 一个sql语句的问题
- unix下备份出来的oracle10.2的库文件在gz后,解压缩后的DMP文件可以还原到windows系统的ORACLE库不
- orcle导入问题
- 对一个表执行SELECT语句后得到一个结果集,如何从这个结果集中得到这个表的所有字段名和所有字段的类型?
- 用oledb连接oracle,汉字字段"等于"判断条件失效!!!
- 关于数据字典问题
- 数据库问题
- oracle9i启动后占用CPU100%,有没有人能解决这个问题?急
- 为什么在pl/sql develop中调试存过速度会那么慢呢?
- Update 的时候关联两个表的时候如何写?
- 请介绍下数据库巨量数据时的分区和分表的区别。
- ORACLE的存储过程,怎样调用JAVA的类啊?
1 aa
2 bb
3 cc
4 dd
1 ee
1 xxx
ID相同的行只显示一条记录并把相同ID的NAME加起来用,隔开
怎么搞???
我需要的结果集是:
id name
1 aa,ee,xxx
2 bb
3 cc
4 dd
---------------------------------
假设你的table 是 tmp_1;
--------------------------------------------
create table tmp_2 as select * from tmp_1 where 1=2;
insert into tmp_2 select distinct id,null from tmp_1;
declare
i number(4);
cursor c_v is select * from ttmp_1;
begin
for v_n in c loop
select count(*) into i from ttmp_2 b where b.name is not null and b.id=v_n.id;
if i=0 then
update ttmp_2 a set name = v_n.name where a.id=v_n.id;
else
update ttmp_2 a set name = name||','||v_n.name where a.id=v_n.id;
end if;
end loop;
end;
/
------------------------------------
或许以下的更有帮助给你看看duanzilin (寻)的文章吧,一个强贴,地址忘记了,还好有保存
主 题: 原创+突发奇想+分享+散分-----关于分组后字段拼接的问题
作 者: duanzilin (寻)
信 誉 值: 120
所属论坛: Oracle 基础和管理
问题点数: 200
回复次数: 29
发表时间: 2005-7-22 11:52:56
最近在论坛上,经常会看到关于分组后字段拼接的问题,
大概是类似下列的情形:
SQL> select no,q from test
2 /NO Q
---------- ------------------------------
001 n1
001 n2
001 n3
001 n4
001 n5
002 m1
003 t1
003 t2
003 t3
003 t4
003 t5
003 t612 rows selected最后要得到类似于如下的结果:
001 n1;n2;n3;n4;n5
002 m1
003 t1;t2;t3;t4;t5;t6 通常大家都认为这类问题无法用一句SQL解决,本来我也这么认为,可是今天无意中突然有了灵感,原来是可以这么做的:
前几天有人提到过sys_connect_by_path的用法,我想这里是不是也能用到这个方法,如果能做到的话,不用函数或存贮过程也可以做到了;要用到sys_connect_by_path,首先要自己构建树型的结构,并且树的每个分支都是单根的,例如1-〉2-〉3-〉4,不会存在1-〉2,1-〉3的情况;
我是这么构建树,很简单的,看下面的结果就会知道了:
SQL> select no,q,rn,lead(rn) over(partition by no order by rn) rn1
2 from (select no,q,row_number() over(order by no,q desc) rn from test)
3 /NO Q RN RN1
---------- ------------------------------ ---------- ----------
001 n5 1 2
001 n4 2 3
001 n3 3 4
001 n2 4 5
001 n1 5
002 m1 6
003 t6 7 8
003 t5 8 9
003 t4 9 10
003 t3 10 11
003 t2 11 12
003 t1 12 12 rows selected有了这个树型的结构,接下来的事就好办了,只要取出拥有全路径的那个path,问题就解决了,先看no=‘001’的分组:
select no,sys_connect_by_path(q,';') result from
(select no,q,rn,lead(rn) over(partition by no order by rn) rn1
from (select no,q,row_number() over(order by no,q desc) rn from test)
)
start with no = '001' and rn1 is null connect by rn1 = prior rn
SQL>
6 /NO RESULT
---------- --------------------------------------------------------------------------------
001 ;n1
001 ;n1;n2
001 ;n1;n2;n3
001 ;n1;n2;n3;n4
001 ;n1;n2;n3;n4;n5上面结果的最后1条就是我们要得结果了
要得到每组的结果,可以下面这样select t.*,
(
select max(sys_connect_by_path(q,';')) result from
(select no,q,rn,lead(rn) over(partition by no order by rn) rn1
from (select no,q,row_number() over(order by no,q desc) rn from test)
)
start with no = t.no and rn1 is null connect by rn1 = prior rn
) value
from (select distinct no from test) tSQL>
10 /NO VALUE
---------- --------------------------------------------------------------------------------
001 ;n1;n2;n3;n4;n5
002 ;m1
003 ;t1;t2;t3;t4;t5;t6对上面结果稍加处理就可以了,希望对大家有帮助:)