2

I'm trying to COUNT attendance_status of drivers according to its value. this is my code as of moment.

SELECT *, COUNT(attendance_status) AS total_cars_dispatched
FROM driver_attendance da 
LEFT JOIN collectible co ON (da.driver_attendance_id=co.driver_attendance_id)
LEFT JOIN driver_pondo dp ON (dp.collectible_id=co.collectible_id)
WHERE attendance_status=19 AND company_id=84 GROUP BY attendance_date DESC

I'd like to know how to make another COUNT of attendance_status when it's value is 4 using a single query.

1
  • Can you post your table structure with sample input data and desired result? Commented Jan 9, 2015 at 7:31

1 Answer 1

2

Try this:

SELECT attendance_date, 
       SUM(CASE WHEN attendance_status = 19 THEN 1 ELSE 0 END) AS total_cars_dispatched, 
       SUM(CASE WHEN attendance_status = 4 THEN 1 ELSE 0 END) AS attendance_status_4
FROM driver_attendance da 
LEFT JOIN collectible co ON da.driver_attendance_id=co.driver_attendance_id
LEFT JOIN driver_pondo dp ON dp.collectible_id=co.collectible_id
WHERE company_id=84 
GROUP BY attendance_date DESC;
Sign up to request clarification or add additional context in comments.

2 Comments

it worked! thanks man, even if i failed to post the structure cause you've already answered it. uhm, additional question, what does the part--> THEN 1 ELSE 0 END meant?
This defines the outcomes of the CASE: 1 for the value actually checked for (here 19 and 4), 0 for anything ELSE. END is the end of the CASE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.