首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

oracle 多条查询记要 结果连接

2012-12-15 
oracle 多条查询记录 结果连接问题是这样:SELECTso.process_instance_id,(SELECT LTRIM(MAX(SYS_CONNECT_B

oracle 多条查询记录 结果连接
问题是这样:
   


SELECT
   so.process_instance_id,
(

   SELECT LTRIM(
   MAX(SYS_CONNECT_BY_PATH(wo, ',')), ',')  
  FROM (SELECT wo, RN, LEAD(RN) OVER(ORDER BY RN) RN1
          FROM (SELECT wo,
                       ROW_NUMBER() OVER(ORDER BY wo DESC) RN
                  FROM (
                           SELECT
                                     
                                     wi.workstep_name wo
                            FROM
                                workitem wi
                            WHERE
                              wi.status IN('I_ASSIGNED','I_AVAILABLE')
                           and wi.process_instance_id=so.process_instance_id
                          
                         )
                 )
         )
 START WITH RN1 IS NULL
CONNECT BY RN1 = PRIOR RN  
   
)  test 
FROM
    serviceordercpemacd so,
    processinstance pi  
WHERE
    so.process_instance_id=pi.process_instance_id

我的test想查到的结果是与serviceordercpemacd 相关的满足条件的所有workitem的名字,以‘,’连接成的字符串。但是我现在的sql是有问题的,错误提示是字符串的结果过长,这个是什么问题呢?或是谁可以给我一个可以使用的代码呢?
其中
SELECT
                                     
                                     wi.workstep_name wo
                            FROM


                                workitem wi
                            WHERE
                              wi.status IN('I_ASSIGNED','I_AVAILABLE')
                           and wi.process_instance_id=so.process_instance_id

返回的结果可能只是一条或两条记录。
[最优解释]
哦。
SELECT pid, max(str)
  FROM (SELECT pid, sys_connect_by_path(wn, ',') AS str
          FROM (SELECT pid,
                       wn,
                       rn,
                       lead(rn) over(PARTITION BY pid ORDER BY rn) rn1
                  FROM (SELECT pid, wn, row_number() over(ORDER BY pid) rn
                          FROM (select t1.pid, t2.workstep_name as wn
                                  from serviceordermace t1
                                  left join workitem t2 on t1.pid = t2.pid)))
        CONNECT BY rn1 = PRIOR rn)
 group by pid
 order by pid
写的太冲忙,自己调整下。
[其他解释]
SELECT pid,LTRIM(MAX(SYS_CONNECT_BY_PATH(workstep_name,',')),',') workstep_name
 FROM (
 SELECT  serviceordermace.pid,
         workitem.workstep_name,
         ROW_NUMBER() OVER(PARTITION BY serviceordermace.pid ORDER BY workitem.workstep_name) GrepSeq
FROM serviceordermace ,workitem
WHERE workitem.pid=serviceordermace.pid
 )
 START WITH GrepSeq=1
 CONNECT BY PRIOR GrepSeq=GrepSeq-1
            AND pid = PRIOR pid
 GROUP BY pid
 ORDER BY 1
[其他解释]
忘了说了,我不能用wm_concat()方法,只能用这种最传统的方法!
[其他解释]
你比我早发了一分钟哈,也在线等呢哈,估计都快下班了大家都没心思了,老兄可否帮我看看我那问题


[其他解释]
直接把数据和想要结果贴出来。帮你写个。
[其他解释]

引用:
直接把数据和想要结果贴出来。帮你写个。

比如说         serviceordermace : pid   
                                     1
                                     2
                                     3
               workitem     pid  workstep_name
                             1        test1
                             1         test2
想要的结果     result       pid  workstep_name
                              1   test1,test2      
[其他解释]
引用:
直接把数据和想要结果贴出来。帮你写个。

高手看过来啊,都折腾一天了,帮帮忙吧!
[其他解释]
WITH  serviceordermace as ( 
SELECT '1' as pid from dual 
union all
SELECT '2' as pid from dual 
union all
SELECT '3' as pid from dual 
),
workitem as ( 
SELECT '1' as pid,'test1' AS workstep_name from dual 
union all
SELECT '1' as pid,'test2' AS workstep_name from dual 
union all
SELECT '2' as pid,'test3' AS workstep_name from dual 
union all
SELECT '2' as pid,'test4' AS workstep_name from dual 
union all
SELECT '2' as pid,'test5' AS workstep_name from dual 
union all
SELECT '3' as pid,'test6' AS workstep_name from dual 
)

select pid,listagg(wn,',') within group(order by pid) as workstep_name from(
select t1.pid,t2.workstep_name as wn from serviceordermace t1 left join workitem t2 on t1.pid = t2.pid) group by pid


-----------------------------
11test1,test2
22test3,test4,test5
33test6

[其他解释]

引用:
WITH  serviceordermace as ( 
SELECT '1' as pid from dual 
union all
SELECT '2' as pid from dual 
union all
SELECT '3' as pid from dual 
),
workitem as ( 
SELECT '1' as pid,'test1'……

listagg() 这个好像是只有11g R2才有的方法吧,俺用不了啊!
[其他解释]
可以用 listagg
[其他解释]
感谢各位的热心帮助,restbely和flighting_sky的方法是OK的,结贴了。

热点排行