Вы можете сделать условную агрегацию. Postgres поддерживает стандартное предложение filter()
, которое здесь пригодится:
select
activity_tag,
bank,
count(*) filter(where within_beyond = 'WITHIN') within_count,
count(*) filter(where within_beyond = 'BEYOND') beyond_count
from mytable
group by activity_tag, bank