3

你怎么能得到一个计数的百分比?尝试根据行数计算百分比时,我得到了所有相同的值。我想要每个属性的百分比取决于有多少 COUNT。因此,如果我计算一个属性 30 次,我希望它根据行数和其他值更改为百分比。

SELECT  Attribute
      , COUNT(Attribute) / COUNT(*) * 100 AS TopIssues
FROM    Audits 
        WHERE MONTH(TransActionDate) = MONTH(GETDATE())
GROUP BY Attribute
ORDER BY COUNT(Attribute) DESC     
4

2 回答 2

3

在 SQL Server 中,您可以使用:

SELECT  Attribute
      , COUNT(Attribute)*1.0 / SUM(COUNT(*)) OVER() * 100 AS TopIssues
FROM    Audits 
WHERE MONTH(TransActionDate) = MONTH(GETDATE())
GROUP BY Attribute
ORDER BY COUNT(Attribute) DESC   
于 2013-10-11T02:27:29.413 回答
0

你也可以这样做:

SELECT  Attribute
      , COUNT(Attribute)*1.0 / ACount AS TopIssues
FROM    Audits 
CROSS JOIN (SELECT COUNT(1) ACount FROM Audits) ACount
WHERE MONTH(TransActionDate) = MONTH(GETDATE())
GROUP BY Attribute, ACount
ORDER BY COUNT(Attribute) DESC   
于 2013-10-11T03:18:02.907 回答