Solved

How to retrieve distinct values from concat field in a group by query

Posted on 2014-03-25
2
764 Views
Last Modified: 2014-03-25
Hi
Assuming the following rows in my table:
ID , Field1, Field2, ConcatField
1  , "test",    3,    "a,b"
2  , "tttt",     2,    "a,c,d"
3  , "test",   8,    "a"
4,  "test",   1,    "d"

Open in new window


I would like to run a query grouped by field1 and showing count(ID), sum(field2) and a field with all values in the concatfield, i.e
Field1, Count, Sum, ConcatField
"test",   3,  12,   "a,b,d"
"tttt",  1,  2,    "a,c,d"

Open in new window


How can i do that?
Thanks
Jaime
0
Comment
Question by:GreatSolutions
2 Comments
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
Use GROUP_CONCAT(), e.g.
SELECT 
  Field1,
  COUNT(*)
  SUM(Field2)
  GROUP_CONCAT(ConcatField)
FROM your_table
GROUP BY Field1;

Open in new window

0
 
LVL 2

Author Comment

by:GreatSolutions
Comment Utility
Many thanks for the fast response!
It gave me duplicates inside the field, but group_concat ( distinct ConcatField ) did the trick!

Jaime
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now