Ameen OT
Ameen OT

Reputation: 170

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.

Upvotes: 0

Views: 764

Answers (2)

Hytool
Hytool

Reputation: 1368

Try Group_Concat,

    SELECT 
            SUM(error_count) AS error_count , 
            GROUP_CONCAT(job_id) AS jobids,
            operator_name 
    FROM
            table_name 
    GROUP BY 
            operator_name

Upvotes: 1

Mippy
Mippy

Reputation: 364

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

Upvotes: 2

Related Questions