dbTalk Databases Forums  

using select to concatenate into a string?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss using select to concatenate into a string? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default using select to concatenate into a string? - 07-21-2006 , 01:33 PM






dilemma: using a select statement ONLY

is it possible to get the results to concatenate in a string such as?

select field from table;

original results
--------------------
1 cat
2 dog
3 mouse

wanted results
---------------------
1 cat, dog, mouse


Reply With Quote
  #2  
Old   
Michel Cadot
 
Posts: n/a

Default Re: using select to concatenate into a string? - 07-21-2006 , 02:06 PM







<kimberly.shaffer (AT) gmail (DOT) com> a écrit dans le message de news: 1153506780.865682.82470 (AT) h48g200...oglegroups.com...
Quote:
dilemma: using a select statement ONLY

is it possible to get the results to concatenate in a string such as?

select field from table;

original results
--------------------
1 cat
2 dog
3 mouse

wanted results
---------------------
1 cat, dog, mouse

Yes, search for "pivot" on asktom.oracle.com or google

Regards
Michel Cadot




Reply With Quote
  #3  
Old   
Charles Hooper
 
Posts: n/a

Default Re: using select to concatenate into a string? - 07-21-2006 , 03:31 PM



kimberly.shaffer (AT) gmail (DOT) com wrote:
Quote:
dilemma: using a select statement ONLY

is it possible to get the results to concatenate in a string such as?

select field from table;

original results
--------------------
1 cat
2 dog
3 mouse

wanted results
---------------------
1 cat, dog, mouse
Take a look at this SQL statement, which allows up to 20 result rows to
be appended together:

SELECT
MAX(DECODE(RN,1,ID,NULL))||
MAX(DECODE(RN,2,', '||ID,NULL))||
MAX(DECODE(RN,3,', '||ID,NULL))||
MAX(DECODE(RN,4,', '||ID,NULL))||
MAX(DECODE(RN,5,', '||ID,NULL))||
MAX(DECODE(RN,6,', '||ID,NULL))||
MAX(DECODE(RN,7,', '||ID,NULL))||
MAX(DECODE(RN,8,', '||ID,NULL))||
MAX(DECODE(RN,9,', '||ID,NULL))||
MAX(DECODE(RN,10,', '||ID,NULL))||
MAX(DECODE(RN,11,', '||ID,NULL))||
MAX(DECODE(RN,12,', '||ID,NULL))||
MAX(DECODE(RN,13,', '||ID,NULL))||
MAX(DECODE(RN,14,', '||ID,NULL))||
MAX(DECODE(RN,15,', '||ID,NULL))||
MAX(DECODE(RN,16,', '||ID,NULL))||
MAX(DECODE(RN,17,', '||ID,NULL))||
MAX(DECODE(RN,18,', '||ID,NULL))||
MAX(DECODE(RN,19,', '||ID,NULL))||
MAX(DECODE(RN,20,', '||ID,NULL))
FROM
(SELECT
ID,
ROWNUM RN
FROM
ACCOUNT
WHERE
ROWNUM<=10
ORDER BY
ID);

The inline view in the SQL statement above just retrieves the first 10
rows of the ACCOUNT table, when sorted alphabetically. The only
critical component here is the ROWNUM, which is given an alias of RN.

Outside the inline view, the MAX and DECODE statements are used to
append the rows together into a single resulting row. The SQL
statement as written will handle up to 20 result rows, even though only
10 are retrieved from the inline view.

The above is SQL statement inspired by Tom Kyte's SQL statement for
finding un-indexed foreign keys. As another posted suggested, you will
likely find some very advanced methods for appending the results into a
single row on the asktom.oracle.com site.

Charles Hooper
PC Support Specialist
K&M Machine-Fabricating, Inc.



Reply With Quote
  #4  
Old   
AT
 
Posts: n/a

Default Re: using select to concatenate into a string? - 07-22-2006 , 12:42 AM



Thanks - I looked on asktom but I couldn't find anything that used
straight sql without a function or that didn't use aggregate or
grouping. I just want to concatenate a number of records from one
field into one string. Two fields won't work.

thx!

kshaffer

Charles Hooper wrote:
Quote:
kimberly.shaffer (AT) gmail (DOT) com wrote:
dilemma: using a select statement ONLY



Reply With Quote
  #5  
Old   
Michel Cadot
 
Posts: n/a

Default Re: using select to concatenate into a string? - 07-22-2006 , 01:10 AM




<kimberly.shaffer (AT) gmail (DOT) com> a écrit dans le message de news: 1153546957.343909.12170 (AT) h48g200...oglegroups.com...
Quote:
Thanks - I looked on asktom but I couldn't find anything that used
straight sql without a function or that didn't use aggregate or
grouping. I just want to concatenate a number of records from one
field into one string. Two fields won't work.

thx!

kshaffer

Concatenate several rows field is this not grouping rows?
What do you mean with your last sentence "Two fields won't work"?

Regards
Michel Cadot




Reply With Quote
  #6  
Old   
Charles Hooper
 
Posts: n/a

Default Re: using select to concatenate into a string? - 07-22-2006 , 05:50 AM



kimberly.shaffer (AT) gmail (DOT) com wrote:
Quote:
Charles Hooper wrote:
kimberly.shaffer (AT) gmail (DOT) com wrote:
dilemma: using a select statement ONLY
Thanks - I looked on asktom but I couldn't find anything that used
straight sql without a function or that didn't use aggregate or
grouping. I just want to concatenate a number of records from one
field into one string. Two fields won't work.

thx!

kshaffer
I know that the syntax in the SQL statement that I provided looks a bit
odd. If you use straight SQL for this task, you must use aggregate
functions to collapse multiple result rows into a single row. With the
help of the DECODE function and the aliased ROWNUM, only one value will
be presented to the MAX function. If you have a table in the database
with a column named ID, try the SQL statement to see if it does what
you need - just replace ACCOUNT with the name of the table. If you
have no tables with a column named ID, replace every ID in the SQL
statement with the appropriate column name.

SELECT
MAX(DECODE(RN,1,ID,NULL))||
MAX(DECODE(RN,2,', '||ID,NULL))||
MAX(DECODE(RN,3,', '||ID,NULL))||
MAX(DECODE(RN,4,', '||ID,NULL))||
MAX(DECODE(RN,5,', '||ID,NULL))||
MAX(DECODE(RN,6,', '||ID,NULL))||
MAX(DECODE(RN,7,', '||ID,NULL))||
MAX(DECODE(RN,8,', '||ID,NULL))||
MAX(DECODE(RN,9,', '||ID,NULL))||
MAX(DECODE(RN,10,', '||ID,NULL))||
MAX(DECODE(RN,11,', '||ID,NULL))||
MAX(DECODE(RN,12,', '||ID,NULL))||
MAX(DECODE(RN,13,', '||ID,NULL))||
MAX(DECODE(RN,14,', '||ID,NULL))||
MAX(DECODE(RN,15,', '||ID,NULL))||
MAX(DECODE(RN,16,', '||ID,NULL))||
MAX(DECODE(RN,17,', '||ID,NULL))||
MAX(DECODE(RN,18,', '||ID,NULL))||
MAX(DECODE(RN,19,', '||ID,NULL))||
MAX(DECODE(RN,20,', '||ID,NULL))
FROM
(SELECT
ID,
ROWNUM RN
FROM
ACCOUNT
WHERE
ROWNUM<=10
ORDER BY
ID);

If the input was:
ID ROWNUM (ROWNUM is an automatically generated column that
increments by 1)
APE 1
CAT 2
CHICKEN 3
DOG 4
MOUSE 5

The output of the above SQL statement would be:
APE, CAT, CHICKEN, DOG, MOUSE

Charles Hooper
PC Support Specialist
K&M Machine-Fabricating, Inc.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.