skip to Main Content

I have this table, in the left there is the sample input table and on the right desired output

sample input output

How to calculate the number of status per empid basis using SQL on MySQL.

2

Answers


  1. Common conditional aggregation:

    SELECT `emp-id`,
           SUM(status = 'Open') `Open`,
           SUM(status = 'Pending') `Pending`,
           SUM(status = 'Resolved') `Resolved`
    FROM source_table
    GROUP BY 1;
    
    Login or Signup to reply.
  2. Try something like this:

    SELECT empid,
           COUNT(CASE WHEN status = 'Open' THEN 1 END) `open`,
           COUNT(CASE WHEN status = 'Pending' THEN 1 END) `pending`,
           COUNT(CASE WHEN status = 'Resolved' THEN 1 END) `resolved`
    FROM sampletable
    GROUP BY empid;
    

    Goodle for "pivot mySQL" for other solutions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search