在Oracle中连接来自SQL查询的结果

我在表格中有这样的数据

NAME PRICE A 2 B 3 C 5 D 9 E 5 

我想在一行中显示所有的值; 例如:

 A,2|B,3|C,5|D,9|E,5| 

我将如何去做一个查询,会给我这样一个string在Oracle? 我不需要将它编入某个东西; 我只是想要一种方法来获得该行出现在结果中,以便我可以复制它,并将其粘贴到word文档中。

我的Oracle版本是10.2.0.5。

– Oracle 10g –

 SELECT deptno, WM_CONCAT(ename) AS employees FROM scott.emp GROUP BY deptno; Output: 10 CLARK,MILLER,KING 20 SMITH,FORD,ADAMS,SCOTT,JONES 30 ALLEN,JAMES,TURNER,BLAKE,MARTIN,WARD 

我知道这有点晚了,但试试这个:

 SELECT LISTAGG(CONCAT(CONCAT(NAME,','),PRICE),'|') WITHIN GROUP (ORDER BY NAME) AS CONCATDATA FROM your_table 

通常当我很快需要类似的东西时,我想在不使用PL / SQL的情况下继续使用SQL,我使用类似于下面的方法:

 select sys_connect_by_path(col, ', ') as concat from ( select 'E' as col, 1 as seq from dual union select 'F', 2 from dual union select 'G', 3 from dual ) where seq = 3 start with seq = 1 connect by prior seq+1 = seq 

这是一个使用“sys_connect_by_path”特殊函数的分层查询,该函数旨在从父项到子项获取“path”。

我们正在做的是模拟seq = 1的logging是seq = 2的logging的父母,所以第四,然后得到最后一个孩子(在这种情况下,loggingseq = 3)的完整path,这将实际上是所有“col”列的连接

适应你的情况:

 select sys_connect_by_path(to_clob(col), '|') as concat from ( select name || ',' || price as col, rownum as seq, max(rownum) over (partition by 1) as max_seq from ( /* Simulating your table */ select 'A' as name, 2 as price from dual union select 'B' as name, 3 as price from dual union select 'C' as name, 5 as price from dual union select 'D' as name, 9 as price from dual union select 'E' as name, 5 as price from dual ) ) where seq = max_seq start with seq = 1 connect by prior seq+1 = seq 

结果是: |A,2|B,3|C,5|D,9|E,5

正如你在Oracle 10g中,你不能使用优秀的listagg() 。 然而,还有许多其他的string聚合技术 。

没有特别需要所有复杂的东西。 假设下表

 create table a ( NAME varchar2(1), PRICE number); insert all into a values ('A', 2) into a values ('B', 3) into a values ('C', 5) into a values ('D', 9) into a values ('E', 5) select * from dual 

不受支持的函数wm_concat应该足够了:

 select replace(replace(wm_concat (name || '#' || price), ',', '|'), '#', ',') from a; REPLACE(REPLACE(WM_CONCAT(NAME||'#'||PRICE),',','|'),'#',',') -------------------------------------------------------------------------------- A,2|B,3|C,5|D,9|E,5 

但是,你也可以在上面的链接中改变汤姆·凯特(Tom Kyte)的stragg ,不用replace的function。

这是另一种方法,使用model子句:

 -- sample of data from your question with t1(NAME1, PRICE) as( select 'A', 2 from dual union all select 'B', 3 from dual union all select 'C', 5 from dual union all select 'D', 9 from dual union all select 'E', 5 from dual ) -- the query select Res from (select name1 , price , rn , res from t1 model dimension by (row_number() over(order by name1) rn) measures (name1, price, cast(null as varchar2(101)) as res) (res[rn] order by rn desc = name1[cv()] || ',' || price[cv()] || '|' || res[cv() + 1]) ) where rn = 1 

结果:

 RES ---------------------- A,2|B,3|C,5|D,9|E,5| 

SQLFiddle示例

pipe理得到,直到这里使用xmlagg:使用oracle 11G从sql小提琴。

数据表:

 COL1 COL2 COL3 1 0 0 1 1 1 2 0 0 3 0 0 3 1 0 SELECT RTRIM(REPLACE(REPLACE( XMLAgg(XMLElement("x", col1,',', col2, col3) ORDER BY col1), '<x>'), '</x>', '|')) AS COLS FROM ab ; 

结果:

 COLS 1,00| 3,00| 2,00| 1,11| 3,10| 

* SQLFIDDLE DEMO

  • 参考阅读XMLAGG

像下面这样,这是非常低效和未经testing的。

  create function foo returning varchar2 as ( declare bar varchar2(8000) --arbitrary number CURSOR cur IS SELECT name,price from my_table LOOP FETCH cur INTO r; EXIT WHEN cur%NOTFOUND; bar:= r.name|| ',' ||r.price || '|' END LOOP; dbms_output.put_line(bar); return bar )