Hvis du bruger Oracle 11g, kan du bruge 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
Hvis du ikke bruger Oracle11g, kan du bruge en aggregeret funktion med en CASE
erklæring:
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