select from same column but multiple values SQL

Viewed 24

My Table is as below:

EmpId   section attn_status
101     Admin       P
102     Admin       P
103     Admin       L
104     Admin       A
105     Store       P
106     Store       L
107     Store       A
108     Security    P
109     Security    L
110     Security    P
111     Security    P

I want to get results like below:

section     Present Absent Late
Admin       2       1       1
Store       1       1       1
Security    3       0       1
1 Answers

This can be done using a group by and sum the values with a condition, like this

select t.section,
       sum(case when t.attn_Status = 'P' then 1 else 0 end) as Present,
       sum(case when t.attn_Status = 'L' then 1 else 0 end) as Late,
       sum(case when t.attn_Status = 'A' then 1 else 0 end) as Absent
from   mytable t
group by t.section

See also this DBFiddle

Related