SQL - 如何计算百分比并添加到查询结果

时间:2016-11-25 13:55:01

标签: sql sql-server tsql group-by

使用此查询后:

SELECT 
    ClientCipher, SUM(LastWeeklyAccessCount) AS ClientAccessCount 
FROM 
    SSLAccessClient 
WHERE 
    ClientCipher IS NOT NULL 
GROUP BY 
    ClientCipher

结果如下:

|---------------------|------------------|
|     ClientCipher    |    AccessCount   |
|---------------------|------------------|
|     TLS 1.0         |         2        |
|     TLS 1.1         |         2        |
|     TLS 1.2         |         4        |
|---------------------|------------------|

如何将使用TLS 1.0,TLS 1.1等的客户端百分比附加到此结果?像这样:

|---------------------|------------------|------------------|
|     ClientCipher    |    AccessCount   |    Percentage    |
|---------------------|------------------|------------------|
|     TLS 1.0         |         2        |      25%         |
|     TLS 1.1         |         2        |      25%         |
|     TLS 1.2         |         4        |      50%         |
|---------------------|------------------|------------------|

任何帮助表示赞赏!谢谢!

5 个答案:

答案 0 :(得分:1)

这种方式不使用Window功能(可能适用于旧版本)。

SELECT
    a. ClientCipher
    , 100.00 * a.ClientAccessCount / b.TotalSum AS Percentage
FROM
    (
        SELECT 
            ClientCipher, SUM(LastWeeklyAccessCount) AS ClientAccessCount 
        FROM 
            SSLAccessClient 
        WHERE 
            ClientCipher IS NOT NULL 
        GROUP BY 
            ClientCipher
    ) a
    CROSS JOIN
    (
        SELECT SUM(LastWeeklyAccessCount) AS TotalSUM
        FROM
            SSLAccessClient
        WHERE
            ClientCipher IS NOT NULL
    ) b;

答案 1 :(得分:1)

SQL DEMO

SELECT 
       DISTINCT
       [ClientCipher],
       -- just for debug
       COUNT([ClientCipher]) over (partition by [ClientCipher]) num,
       COUNT([ClientCipher]) over () den,

       -- your percentage
       COUNT([ClientCipher]) over (partition by [ClientCipher]) *1.0 /
       COUNT([ClientCipher]) over () perc
FROM Table1

<强>输出

enter image description here

答案 2 :(得分:1)

另一种选择可能是

;with cte as (
        SELECT  ClientCipher, SUM(LastWeeklyAccessCount) AS ClientAccessCount 
        FROM    SSLAccessClient 
        WHERE   ClientCipher IS NOT NULL 
        GROUP BY  ClientCipher
)
Select *
      ,Percentage = ClientAccessCount * 100.0 / (Select sum(ClientAccessCount) from cte)
 From  cte

答案 3 :(得分:1)

你应该可以使用聚合函数来计算总数

    SELECT 
        ClientCipher, 
        SUM(LastWeeklyAccessCount) AS ClientAccessCount , 
        (SUM(LastWeeklyAccessCount) / SUM(LastWeeklyAccessCount) OVER () ) * 100
    FROM 
        SSLAccessClient 
    WHERE 
        ClientCipher IS NOT NULL 
    GROUP BY 
        ClientCipher

答案 4 :(得分:0)

使用窗口函数很容易:

SELECT ClientCipher, SUM(LastWeeklyAccessCount) AS ClientAccessCount ,
       SUM(LastWeeklyAccessCount) / SUM(SUM(LastWeeklyAccessCount)) OVER () as Percentage
FROM SSLAccessClient 
WHERE ClientCipher IS NOT NULL 
GROUP BY ClientCipher;

这是编写查询的最佳方式。

请注意,SQL Server会进行整数除法,因此您可能需要转换为数字。所以,你想要的表达式可能是:

       SUM(LastWeeklyAccessCount) * 100.0 / SUM(SUM(LastWeeklyAccessCount)) OVER () as Percentage