Home » SQL & PL/SQL » SQL & PL/SQL » Nice question, any answer plsss.
Nice question, any answer plsss. [message #7368] |
Mon, 09 June 2003 05:00 |
sachin kumar gupta
Messages: 157 Registered: March 2003
|
Senior Member |
|
|
Hi,
I m doing this:
select column from tablename;
column
-------
we
df
fdg
dfg
dd
My question is:
I want any SQL script which converts my o/p as:
we,df,fdg,dfg,dd
Hope u got my question.
Kindly help me.
TIA,
Sachin
|
|
|
Re: Nice question, any answer plsss. [message #7370 is a reply to message #7368] |
Mon, 09 June 2003 09:22 |
Art Metzer
Messages: 2480 Registered: December 2002
|
Senior Member |
|
|
Sachin,
You have a couple of options here.
If you know you will never have more than, for example, eight rows that you will want to combine into one, then you could writeSQL> COLUMN converted FORMAT A40
SQL> SELECT MAX(DECODE(pivot.rn,1,pivot.my_column))
2 || MAX(DECODE(pivot.rn,2,',' || pivot.my_column))
3 || MAX(DECODE(pivot.rn,3,',' || pivot.my_column))
4 || MAX(DECODE(pivot.rn,4,',' || pivot.my_column))
5 || MAX(DECODE(pivot.rn,5,',' || pivot.my_column))
6 || MAX(DECODE(pivot.rn,6,',' || pivot.my_column))
7 || MAX(DECODE(pivot.rn,7,',' || pivot.my_column))
8 || MAX(DECODE(pivot.rn,8,',' || pivot.my_column)) converted
9 FROM (SELECT ord_tbl.my_column
10 , ROWNUM rn
11 FROM (SELECT my_column
12 FROM my_table
13 ORDER BY my_column) ord_tbl) pivot
14 /
CONVERTED
----------------------------------------
dd,df,dfg,fdg,we If you can't be sure of any maximum limit, then you could always wrap a cursor specific to your table and column in a function:SQL> CREATE OR REPLACE FUNCTION to_row
2 RETURN VARCHAR2
3 IS
4 CURSOR c_columns IS
5 SELECT mt.my_column
6 FROM my_table mt
7 ORDER BY mt.my_column;
8
9 l_string VARCHAR2(4000);
10 BEGIN
11 FOR sep_by_commas IN c_columns LOOP
12 l_string := l_string
13 || sep_by_commas.my_column
14 || ',';
15 END LOOP;
16 RETURN(SUBSTR(l_string,1,LENGTH(l_string) - 1));
17 END to_row;
18 /
Function created.
SQL> SELECT to_row AS converted FROM DUAL;
CONVERTED
----------------------------------------
dd,df,dfg,fdg,we
SQL> If you are on at least 9i, you will be interested in the following link from "Ask Tom":Good luck, Sachin.
A
|
|
|
Goto Forum:
Current Time: Thu Apr 25 07:07:32 CDT 2024
|