我有一个类似于下面的 SELECT 语句,它在一个查询中返回多个计数。
SELECT invalidCount = (SELECT COUNT(*) FROM <...a...> WHERE <...b...>),
unknownCount = (SELECT COUNT(*) FROM <...c...> WHERE <...d...>),
totalCount = (SELECT COUNT(*) FROM <...e...> WHERE <...f...>)
这工作正常,但我想向 SELECT 添加两个百分比列:
invalidCount * 100 / totalCount AS PercentageInvalid,
unknownCount * 100 / totalCount AS UnknownPercentage
如何修改我的 SELECT 语句来处理这个问题?
最佳答案
您可以在 from
子句中使用子查询:
select
s.invalidCount,
s.unknownCount,
s.totalCount,
invalidCount * 100 / s.totalCount as PercentageInvalid,
unknownCount * 100 / s.totalCount as PercentageUnknown
from
(select invalidCount = (SELECT COUNT(*) FROM <...a...> WHERE <...b...>),
unknownCount = (SELECT COUNT(*) FROM <...c...> WHERE <...d...>),
totalCount = (SELECT COUNT(*) FROM <...e...> WHERE <...f...>)) s
关于sql - 在一个 SQL SELECT 查询中为多个计数添加百分比,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1306112/