I have a SELECT statement similar to the one below which returns several counts in one query.
SELECT invalidCount = (SELECT COUNT(*) FROM <...a...> WHERE <...b...>),
unknownCount = (SELECT COUNT(*) FROM <...c...> WHERE <...d...>),
totalCount = (SELECT COUNT(*) FROM <...e...> WHERE <...f...>)
This works fine but I wanted to add two percentage columns to the SELECT:
invalidCount * 100 / totalCount AS PercentageInvalid,
unknownCount * 100 / totalCount AS UnknownPercentage
How do I modify my SELECT statement to handle this?
You can use a subquery in the from clause:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With