I have the following query that selects teams/filters, and count how many associations match them

```
select t.name, count(c.id) from teams t
left join users u on t.id = u.team_id
left join conversation_users cu on cu.user_id = u.id
left join conversations c on cu.conversation_id = c.id
group by t.id
UNION
select 'Me', count(cu.id) from conversations c
left join conversation_users cu on cu.conversation_id = c.id
where cu.user_id = 'logged in user'
UNION
select 'All', count(c.id) from conversations c
left join apps a on c.app_id = a.id
where a.id = 'current app'
UNION
select 'Unassigned', count(c.id) from conversations c
left join apps a on c.app_id = a.id
where a.id = 'current app' and c.user_id is null

```

I'm not database genius, but this seems like a very inefficient/not scaleable approach. Is there a better way to accomplish this?

And currently it takes about 50ms to run on a small size table.

I figured I could fetch the Me, All and Unsigned filters very quickyl (around 15ms) with this approach

```
select
    count(case when c.started then 1 end) as all,
    count(case when COALESCE(c.assigned_user_id::text, c.assigned_team_id::text) is null and c.started then 1 end) as unassigned,
    count(case when c.assigned_user_id = 'clt8ojxvk0000dp2fyvwq126' and c.started then 1 end) as me
from apps a
left join conversations c on c.app_id = a.id
where a.id = 'ASnYW1-RgCl0I'

```

Could I do something similar for the teams and merge them together?