create table account (
id int
);
insert into account values (333);
create table posts (
accountId int,
inbound tinyint
);
insert into posts values (333, 1), (333, 0), (333, 0);
select count(*) as total,
(select count(*) from posts where inbound = 0 and accountid = 333) as inbound,
(select count(*) from posts where inbound = 1 and accountid = 333) as outbound
from account a
join posts p on p.accountId = a.id where a.id = 333
group by a.id;
with counts as (
select
count(*) as total,
count(nullif(inbound, 0)) as inbound,
count(nullif(inbound, 1)) as outbound
from posts
where accountid = 333
) select
* ,
round((outbound / total) * 100) outboundPercentage
from counts;