表结构式这样的:
CREATE TABLE t_row_str(
ID INT,
col VARCHAR2(10));
INSERT INTO t_row_str VALUES(1,'a');
INSERT INTO t_row_str VALUES(2,'b');
INSERT INTO t_row_str VALUES(3,'c');
INSERT INTO t_row_str VALUES(4,'a');
INSERT INTO t_row_str VALUES(5,'d');
INSERT INTO t_row_str VALUES(6,'e');
INSERT INTO t_row_str VALUES(7,'c');
COMMIT;
SELECT * FROM t_row_str;多行转字符串是这样的:
适用范围:8i,9i,10g及以后版本
SELECT t.id id, MAX(substr(sys_connect_by_path(t.col, ','), 2)) str
FROM (SELECT id, col, row_number() over(PARTITION BY id ORDER BY col) rn
FROM t_row_str) t
START WITH rn = 1
CONNECT BY rn = PRIOR rn + 1
AND id = PRIOR id
GROUP BY t.id;
问题是这个方法转化后为什么每个字符串只有3个字符? 怎么改才能够让所有行转化为一个字符串?? 100分求教

解决方案 »

  1.   

    你想要啥效果?
    最后输出 '1,2,3,4,5,6', 'a,b,c,d,e,f'?
      

  2.   

    只要最后结果输出‘a,b,c,d,e,f’ 就可以了
      

  3.   

    拼凑的,也没有优化,实用性不大,只是凑个热闹,顶一下,哈哈select max(a)||','||max(b)||','||max(c)||','||max(d)||','||max(e) rs from
    (
        select decode(max(rn),1,'a','') a ,decode(max(rn),2,'b','') b,decode(max(rn),3,'c','') c,decode(max(rn),4,'d','') d,decode(max(rn),5,'e','') e
        from
        (
            select rownum rn , col from
            (
            select distinct col from t_row_str order by 1
            )
        )
        group by col
    )
      

  4.   

    10g直接用
    select wm_concat(col) from t_row_str order by col;
      

  5.   

    不过楼主貌似你那个语句的结果啥都没变
    SQL> SELECT t.id id, MAX(substr(sys_connect_by_path(t.col, ','), 2)) str
      2  FROM (SELECT id, col, row_number() over(PARTITION BY id ORDER BY col) rn
      3  FROM t_row_str) t
      4  START WITH rn = 1
      5  CONNECT BY rn = PRIOR rn + 1
      6  AND id = PRIOR id
      7  GROUP BY t.id;        ID STR
    ---------- --------------------
             1 a
             2 b
             3 c
             4 a
             5 d
             6 e
             7 c已选择7行。
      

  6.   


    SQL>  select wm_concat(f.str) from (SELECT t.id id, MAX(substr(sys_connect_by_path(t.col, ','),2)) str
      2   FROM (SELECT id, col, row_number() over(PARTITION BY col ORDER BY col) rn
      3   FROM t_row_str) t
      4   START WITH rn = 1
      5   CONNECT BY rn = PRIOR rn + 1
      6   AND id = PRIOR id
      7   GROUP BY t.id) f order by f.str;WM_CONCAT(F.STR)
    --------------------------------------------------------------------------------
    a,b,c,d,e
      

  7.   

    你的id是1~7是不同的,你group by id 自然就是每个显示一个了。[TEST@ora10gr1#2009-12-18/18:09:30] SQL>select * from t_row_str;        ID COL
    ---------- ----------
             1 a
             2 b
             1 a
             1 b
             1 c
             2 a
             2 d
             2 e
             2 c9 rows selected.[TEST@ora10gr1#2009-12-18/18:09:38] SQL>SELECT t.id id, MAX(substr(sys_connect_by_path(t.col, ','),
      2  FROM (SELECT id, col, row_number() over(PARTITION BY id ORDER BY col) rn
      3  FROM t_row_str) t
      4  START WITH rn = 1
      5  CONNECT BY rn = PRIOR rn + 1
      6  AND id = PRIOR id
      7  GROUP BY t.id;        ID STR
    ---------- ----------
             1 a,a,b,c
             2 a,b,c,d,e
      

  8.   

    SELECT t.id id, MAX(substr(sys_connect_by_path(t.col, ','), 2)) str 
    FROM (SELECT id, col, row_number() over(PARTITION BY id ORDER BY col) rn 
    FROM t_row_str) t 
    START WITH rn = 1 
    CONNECT BY rn = PRIOR rn + 1 
    AND id = PRIOR id 
    GROUP BY t.id; 
    上面这个查询是没有问题的
    如果按照楼主给出的数据1,2,3,4,5,6,7那么上面的查询语句是看不出效果的
    因为在OVER窗口函数中指定了partition by id,可你的7条记录7个id都不一样 所以感觉没什么效果
    重新建几条数据你看看
    SQL> select * from t_row_str;        ID COL
    ---------- ----------
             1 a
             1 b
             1 c
             2 a
             2 d
             2 e
             3 c7 rows selected.
    SQL> SELECT t.id id, MAX(substr(sys_connect_by_path(t.col, ','), 2)) str
      2  FROM (SELECT id, col, row_number() over(PARTITION BY id ORDER BY col) rn
      3  FROM t_row_str) t
      4  START WITH rn = 1
      5  CONNECT BY rn = PRIOR rn + 1
      6  AND id = PRIOR id
      7  GROUP BY t.id;
            ID STR
    ---------- ------------------------------
             1 a,b,c
             2 a,d,e
             3 c
      

  9.   

    PARTITION BY id这个要去掉,否则由于id不重复,group没有意义加上去重,10g可以用
    select wm_concat(distinct t.col)
    from t_row_str t9i
    SELECT  MAX(substr(sys_connect_by_path(t.col, ','), 2)) str 
    FROM (SELECT distinct col, dense_rank() over(ORDER BY col) rn 
    FROM t_row_str) t 
    START WITH rn = 1 
    CONNECT BY rn = PRIOR rn + 1 可能你需要根据某个字段来分组,但这里的id显然不是这么个字段