Two select count queries and then calculate percentage

前端 未结 1 697
日久生厌
日久生厌 2021-01-22 02:12

I\'d like to combine two queries

SELECT COUNT(*) FROM abc;
SELECT COUNT(Status) FROM ABC WHERE Status=\'Active\';

And then calculate the percen

相关标签:
1条回答
  • 2021-01-22 02:51

    This should give you what you want:

    SELECT
        COUNT(*) AS TotalCount,
        SUM(IIF(Status = 'Active', 1, 0)) AS ActiveCount,
        ROUND((SUM(IIF(Status = 'Active', 1, 0)) * 100/ COUNT(*)),2) AS PctActive
    FROM
        Abc
    

    EDIT: Didn't notice that this was for Access. I don't know if CAST is available in Access, so you may need to use an equivalent function to make sure that the integers don't simply yield 1 or 0. It's possible that Access will convert a division into a decimal automatically, but in SQL Server it does not.

    0 讨论(0)
提交回复
热议问题