1

Here is my current query (confidential data replaced and where clauses removed for simplicity):

select a.id, group_concat(v.value) from asset a, values v where a.id=v.id group by a.case_id;

The current results appear as:

a.id   v.value
123   a,b
234   a
456   a,b,c
789   d,e
237   a

What I want it to do is group by and count the results of the group_concat, so it should look like:

v.value    count
a,b           1
a              2
a,b,c        1
d,e           1

I have tried unsuccessfully to group on the field name I give to the group_concat, it keeps on saying 'unable to group by 'name'

I have to do this directly in the query not in the code. Any suggestions?

2
  • Why don't you want case_id to appear in the result set? Commented Jan 31, 2013 at 17:12
  • my mistake, it should have been group by a.id not a.case_id. I don't need the individual id in the result set. Commented Jan 31, 2013 at 17:23

2 Answers 2

3
SELECT gc_values
     , COUNT(*)
  FROM 
     ( SELECT a.id
            , GROUP_CONCAT(v.value) gc_values
         FROM asset a
         JOIN `values` v 
           ON v.id = a.id
        GROUP
           BY a.id
     ) x
 GROUP
    BY gc_values;
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the quick response. However, your code appears to count the number of results for each individual value of the group_concat. What I want is the grouped results of the group_concat. In other words, how many times does just 'a' appear, or how may times do we have 'a,b,c'
Er, no. I'm pretty sure this fulfils the spec. !?!
Interesting. However, I'm getting a syntax error near ' COUNT(*). That's even when I copy your exact code
Oh yeah - values is a reserved word in 5.6
0

you could do:

select grouped_values, count(*) from [subselect aliasing group_concat] group by grouped_values

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.