MySQL Forums
Forum List  »  General

trying to combine rows
Posted by: Dave Dewire
Date: June 19, 2014 02:43PM

Im trying to write a query that finds duplicate names and then gives me data associated with them combining rows into a commas separated string. I know what I want, I just cant figure out how to get there. The database structure cant be changed so I have to figure out how to do it with a query.

select M.name, syncdate ,DUP.c, L.name as Label
from MACHINE as M
left join MACHINE_LABEL_JT as ML on ML.machine_id=M.ID
left join LABEL as L on L.ID=ML.label_id and not L.type = 'hidden'
left join (
/* get all duplicate names */
SELECT name,count(*) as c FROM ORG1.MACHINE
group by name
having c>1
) as DUP on DUP.name=M.Name
where c>1
group by name

This gives me
name1 1/1/2014 2 label1
name1 1/1/2014 2 label2
name1 1/9/2014 2 label1
name2 1/1/2014 3 label1
name2 1/9/2014 3 label1
name2 1/9/2014 3 label2
name2 1/9/2014 3 label4
name2 1/15/2014 3 label2

What I need to do is combine the labels for each unique name,syncdate into one comma delimited string. It needs to look like this:
name1 1/1/2014 2 label1,label2
name1 1/9/2014 2 label1
name2 1/1/2014 3 label1
name2 1/9/2014 3 label1,label2,label4
name2 1/15/2014 3 label2

Any help would be appreciated.

Options: ReplyQuote


Subject
Written By
Posted
trying to combine rows
June 19, 2014 02:43PM
June 19, 2014 02:51PM
June 19, 2014 03:00PM
August 18, 2014 12:55AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.