Home > Ask the Oracle Experts > Questions & Answers
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Concatenate values into comma-delimited string

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 14 February 2005
I have a table with CompanyID, EmployeeID, and RoleID. There are a number of records where the Role is different for the same employee at a company (e.g. John Smith at ABC Inc. is the owner, decision maker and purchasing agent). I need to create a list of roles of each employee at a specific company. I would like the list to concatenate into one comma delimited string.

>
EXPERT RESPONSE

This is a common and quite reasonable request. The answer is pretty straightforward, too. Unless you are on a database system which has the functionality built in, like MySQL version 4.1 or Sybase ASE, you can't do this with SQL.

Let's say you have a simple query, like this:

select CompanyID
     , EmployeeID
     , RoleID
  from yourtable
order
    by CompanyID
     , EmployeeID
     , RoleID

And let's say it returns this result:

CompanyID EmployeeID RoleID
   ABC       1003      23
   ABC       1005       1
   ABC       1005       9
   ABC       1005      37
   DEF        442       9   
   DEF        442      45   
   DEF        448      11

In most situations, you would have to write application code to:

  1. loop over the result set

  2. initialize control break variables for CompanyID and EmployeeID

  3. initialize a string for the roles

  4. concatenate each row's RoleID to the string

  5. detect control breaks and output the concatenated string

  6. don't forget to output the last line after end of file!

In MySQL 4.1, though, using the GROUP_CONCAT() function, it's drop dead simple by comparison:

select CompanyID
     , EmployeeID
     , group_concat(RoleID
           order by RoleID) as Roles
  from yourtable
group
    by CompanyID
     , EmployeeID
order
    by CompanyID
     , EmployeeID

Here's what the above query returns:

CompanyID EmployeeID Roles
   ABC       1003      23
   ABC       1005      1,9,37
   DEF        442      9,45   
   DEF        448      11

Neat, eh?


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts