Skip to content
Advertisement

How can I use 3 group by in one query statement sql? [closed]

This is my query.Group by query in one table and I wanna get three sum column in select statement

This is my table

This is what I really want

Advertisement

Answer

You can use CASE statement to achieve this result as shown below.

SELECT sales_id,  
       SUM(CASE WHEN type = 'deposit' THEN amount ELSE 0 END) AS deposit,
       SUM(CASE WHEN type = 'withdrawn' THEN amount ELSE 0 END) AS withdrawn,
       SUM(CASE WHEN type = 'commission' THEN amount ELSE 0 END) AS commission
FROM `ds_wd20_files` GROUP BY sales_id

Please replace sales_id with sales person name

8 People found this is helpful
Advertisement