0

i have a query below

select emp_id,
  sum(case when leave_type = 'Planned' then 1 else 0 end) Planned,
  sum(case when leave_type = 'Not Informed' then 1 else 0 end) NotInformed,
  sum(case when leave_type = 'Informed' then 1 else 0 end) Informed
from table where activity_type='Leave'
group by emp_id

it gives below result

enter image description here

how to get count for above columns for 567 it will display 3 and for 619 it will display 4 in separate column please help

0

Add a COUNT() column:

select emp_id,
  sum(case when leave_type = 'Planned' then 1 else 0 end) Planned,
  sum(case when leave_type = 'Not Informed' then 1 else 0 end) NotInformed,
  sum(case when leave_type = 'Informed' then 1 else 0 end) Informed,
  count(*) as Total
from t
where activity_type='Leave'
group by emp_id;
emp_id | planned | notinformed | informed | total
-----: | ------: | ----------: | -------: | ----:
   619 |       2 |           1 |        1 |     4
   567 |       0 |           3 |        0 |     3

db<>fiddle here

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.