Redone
Redone

Reputation: 1313

Query to get data, group by two columns and display multiple similar row by Client ID in one row

I have a mysql table as in below figure.

enter image description here

I want to query to this table so that I get data as follows:

enter image description here

I am using the following query.

SELECT SUM(TOTAL_HOURS) AS TOTAL_HOURS, CLIENT_ID, `DAY` 
FROM timesheet_slave
GROUP BY CHARGE_ID, `DAY`;

And getting result as below:

enter image description here

How do I merge similar row with client id and get data as in following table?

enter image description here

Any help would be appreciated. Thanks

Upvotes: 0

Views: 69

Answers (1)

Giorgos Betsos
Giorgos Betsos

Reputation: 72185

You can use conditional aggregation for this as shown below:

SELECT CLIENT_ID,
       SUM(IF(`DAY`='Working', TOTAL_HOURS, 0)) AS TOTAL_WORKING_HOURS,   
       SUM(IF(`DAY`='Holiday', TOTAL_HOURS, 0)) AS TOTAL_HOLIDAY_HOURS
FROM timesheet_slave  
GROUP BY CLIENT_ID ;

Upvotes: 1

Related Questions