Om du använder Oracle 11g kan du använda PIVOT
funktion:
select *
from
(
select tkey, status,
status as col
from tableB b
left join tableA a
on a.fkey = b.fkey
) src
pivot
(
count(status)
for col in ('20' as Count_Status20,
'30' as Count_Status30,
'40' as Count_Status40)
) piv;
Se SQL Fiddle with Demo
Om du inte använder Oracle11g kan du använda en aggregatfunktion med en CASE
uttalande:
select tkey,
count(case when status = 20 then 1 else null end) as Count_Status20,
count(case when status = 30 then 1 else null end) as Count_Status30,
count(case when status = 40 then 1 else null end) as Count_Status40
from tableB b
left join tableA a
on b.fkey = a.fkey
group by tkey
Se SQL Fiddle with Demo