1

如何计算整列的百分比?目前我到目前为止所写的内容只给了我每行的百分比。我只想要 1 行的总百分比。我想将所有值相加以计算一个百分比。

SELECT TOP 1000
       CONVERT(DECIMAL(10, 2), CAST(Passed AS DECIMAL(10, 2))
        / ( Actual ) * 100) AS Percent
FROM    Audits
WHERE   
        MONTH(TransActionDate) = MONTH(GETDATE())
GROUP BY Actual
     , Passed
      , transactiondate
ORDER BY TransActionDate DESC
4

1 回答 1

1

尝试使用AVG()并从GROUP BY子句中删除字段:

SELECT AVG(CONVERT(DECIMAL(10, 2), CAST(Passed AS DECIMAL(10, 2))
        / ( Actual ) * 100)) AS Percent
FROM Audits
WHERE MONTH(TransActionDate) = MONTH(GETDATE())

如果需要TOP 1000,请使用子查询:

SELECT AVG(Percent)
FROM (
    SELECT TOP 1000
       CONVERT(DECIMAL(10, 2), CAST(Passed AS DECIMAL(10, 2))
        / ( Actual ) * 100) AS Percent
    FROM Audits
    WHERE MONTH(TransActionDate) = MONTH(GETDATE())
    ORDER BY TransActionDate DESC
    ) t
于 2013-10-11T01:32:17.877 回答