user3619389 user3619389 - 2 years ago 55
SQL Question

How to get multiple values in single column when using group by?

My current query is

SELECT SUM(error_count) as error_count, job_id, operator_name
from table_name
GROUP BY operator_name

And the output is

error_count job_id operator_name
17 12321 A.kumar
11 2212 Bindu
45 3412 Harish Babu

This is coming correctly for error count, but job id which is coming as the last job_id is not correct. I want all the job_id in comma seperated

SELECT SUM(error_count) as error_count , job_id = some_func(""+job_id), operator_name
from table_name
GROUP BY operator_name

So I can get the answer as

error_count job_id operator_name
17 12321,123 A.kumar
11 2212 ,111 Bindu
45 3412,3412 Harish Babu

I hope this question makes sense.

Answer Source

Try this, may work;)

SELECT SUM(error_count) as error_count , group_concat(job_id) as job_id, operator_name    
FROM table_name
GROUP BY operator_name

Also, take a look of GROUP BY (Aggregate) Function Descriptions

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download