Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Aggregate In Oracle 10g

Status
Not open for further replies.

matrun

IS-IT--Management
Jan 13, 2004
26
GB
Hello there - could anyone help me with a quick question on string aggregation.
Is it possible to write some SQL on the fly (i.e. not PL/SQL) that would aggregate multiple rows into one column.
For example imagine a table called people, with columns name and city; how would I amend the query below to show in column 1 the name of a city and then in column 2 a comma-delim list of people [names] living in that city:

select name,city
from people
group by city

so instead of:

name city
jack london
sophie london
eric london
etc.

you would see

city name
london jack,sophie,eric

any ideas?

Regards

MAtt Rundle
 

Try this:
Code:
col names format a35
SELECT     city, SUBSTR (MAX (SYS_CONNECT_BY_PATH (NAME, ',')), 2) names
      FROM (SELECT city, NAME,
                   ROW_NUMBER () OVER (PARTITION BY city ORDER BY NAME) rn
              FROM people)
START WITH rn = 1
CONNECT BY PRIOR rn = rn - 1 AND PRIOR city = city
  ORDER BY city
/
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks! Fantastic. Thanks so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top