問?: 表: 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是數字型。這兩個字段的唯一值的個數是未知的 不知該如何才能做到,請各位高手幫忙。 答!: 1: id name 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-2211: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 t6 12 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 12 001 n4 23 001 n3 34 001 n2 45 001 n1 5 002 m1 6 003 t6 78 003 t5 89 003 t4 910 003 t3 1011 003 t2 1112 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) t SQL> 10/ NO VALUE ------------------------------------------------------------------------------------------ 001 ;n1;n2;n3;n4;n5 002 ;m1 003 ;t1;t2;t3;t4;t5;t6 對上面結果稍加處理就可以了,希望對大家有幫助:) 答!: 2: 注意注意 sys_connect_by_path有長度限制,不能超過4K