I have data like this in a table
NAME PRICE
A 2
B 3
C 5
D 9
E 5
I want to display all the values in one row; for instance:
A,2|B,3|C,5|D,9|E,5|
How would I go about making a query that will give me a string like this in Oracle? I don't need it to be programmed into something; I just want a way to get that line to appear in the results so I can copy it over and paste it in a word document.
My Oracle version is 10.2.0.5.
Something like the following, which is grossly inefficient and untested.
I know this is a little late but try this:
Usually when I need something like that quickly and I want to stay on SQL without using PL/SQL, I use something similar to the hack below:
It's a hierarchical query which uses the "sys_connect_by_path" special function, which is designed to get the "path" from a parent to a child.
What we are doing is simulating that the record with seq=1 is the parent of the record with seq=2 and so fourth, and then getting the full path of the last child (in this case, record with seq = 3), which will effectively be a concatenation of all the "col" columns
Adapted to your case:
Result is:
|A,2|B,3|C,5|D,9|E,5
As you're in Oracle 10g you can't use the excellent
listagg()
. However, there are numerous other string aggregation techniques.There's no particular need for all the complicated stuff. Assuming the following table
The unsupported function
wm_concat
should be sufficient:But, you could also alter Tom Kyte's
stragg
, also in the above link, to do it without the replace functions.-- Oracle 10g --
Here is another approach, using
model
clause:Result:
SQLFiddle Example