How to get approved , unapproved , deleted count from the below result using sql query.

SELECT approved,deleted_at,count(id)AS count FROM `payment_voucher` 
    WHERE company_id = 1 GROUP BY approved,deleted_at;

I'm attaching the above-queried result as an image.

enter image description here

Desired output: Approved:32 not_approved:4 deleted:7

SELECT SUM(CASE WHEN (approved=1) AND (deleted_at IS NULL) THEN `count` END) approved,
       SUM(CASE WHEN (approved=0) AND (deleted_at IS NULL) THEN `count` END) not_approved,
       SUM(CASE WHEN deleted_at IS NOT NULL THEN `count` END) deleted
FROM (your subquery) source
| improve this answer | |
  • thank you Akina, it worked – Navaneet Raman Bhaskar Dec 26 '19 at 13:09
  • Please mark as the answer. – PdC Dec 26 '19 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.