user2020327
user2020327

Reputation: 1

mysql Group by in nested query

I have employee_id,status and payment field. I need to produce two different output ( sum(payment)) with two different condition. The problem is when i use group by, it produce the total sum of all rows, whilst I want it to group by the employee id.

this is my query:

select employee_id, 
(select sum(payment) from tblempay where status=1) AS 'Total 1',
(select sum(payment) from tblempay where status=2) AS 'Total 2'
from tblempay
group by employee_id

hope somebody can help me. Thank you.

Upvotes: 0

Views: 75

Answers (1)

Blank
Blank

Reputation: 12378

Try this;)

select employee_id, 
sum(if(status=1, payment, 0)) AS `Total 1`,
sum(if(status=2, payment, 0)) AS `Total 2`
from tblempay
group by employee_id

Upvotes: 1

Related Questions