I have two tables. Table A contains a list of departments, there are 20 in total.
See image
SELECT
A.DEPTNAME,
(SELECT COUNT(*) FROM TABLEA A2 INNER JOIN TABLEB B ON B.TableAID = A2.TableAID WHERE B.EMPLOYEE_CLASS = 'Full time' AND A2.TableAID = A.TableAID) FullTimeNo,
(SELECT COUNT(*) FROM TABLEA A2 INNER JOIN TABLEB B ON B.TableAID = A2.TableAID WHERE B.EMPLOYEE_CLASS = 'Part time' AND A2.TableAID = A.TableAID) PartTimeNo
FROM
TABLEA
You want a PIVOT
select *
from (tableb) s
pivot (Max(staffno) for employee_class in ([Full Time],[Part Time])) p
Try this:
select A.DEPTNAME,
sum(case when B.EMPLOYEE_CLASS='Full time' then B.STAFF_NO else 0) as FullTimeNo,
sum(case when B.EMPLOYEE_CLASS='Part time' then B.STAFF_NO else 0) as PartTimeNo,
from TabA A
join TabB B on A.DEPTNAME=B.DEPTNAME
group by A.DEPTNAME