Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Hinzufügen von Prozentsätzen zu mehreren Zählungen in einer SQL SELECT-Abfrage

Sie können eine Unterabfrage im from verwenden Klausel:

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