【发布时间】:2022-01-23 15:56:50
【问题描述】:
我有一个查询,它返回一些数据计数如下,基本上这个表是posts 并且将包含inbound 或outbound 帖子。我想做一个查询,返回出站帖子的百分比。
例如
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;
这将返回类似以下内容...
+-------+---------+----------+
| total | inbound | outbound |
+-------+---------+----------+
| 802 | 525 | 277 |
+-------+---------+----------+
如何修改上面的查询,使其返回一个额外的列来计算总数的出站百分比,例如(277 / 802) * 100 例如(outbound / total) * 100)
那么结果中带有新列的预期输出将如下四舍五入到最接近的整数?
+-------+---------+----------+---------------------+
| total | inbound | outbound | outboundPercentage |
+-------+---------+----------+---------------------+
| 802 | 525 | 277 | 35 |
+-------+---------+----------+---------------------|
【问题讨论】: