Я бы просто использовал условную агрегацию:
select name,
( max(case when type = 'count' then count end) /
max(case when type = 'total' then count end)
) as average
from t
group by name;
На самом деле, если предположить, что total
> = count
, то это еще проще:
select name, min(count) / max(count) as average
from t
group by name;
Если Вы действительно предпочли join
над group by
:
select t.name, tc.count / tt.count
from t tc join
t tt
on tc.name = tt.name and
tc.type = 'count' and
tt.type = 'total';