用于SUM计算列的SQL语法,用于计算百分比分布

时间:2015-04-06 11:11:34

标签: sql sql-server count sql-server-2014

我正在使用SQL Server 2014,我有以下查询运行良好:

USE MyDatabase

SELECT [Room Nights],
       COUNT([Room Nights]) AS 'Count of RN'
FROM HOLDINGS2
GROUP BY [Room Nights]

输出如下:

Room Nights      Count of RN
 1                 3
 4                 10
 5                 6
 7                 25

现在我想显示另一个列,它给出了Count of RN的百分比分布。因此,我的输出必须是这样的:

Room Nights      Count of RN    % Distribution
     1                 3           6.8
     4                 10          22.7
     5                 6           13.6
     7                 25          56.8

我看了下面的讨论,试图找到一个解决方案: percent distribution with counted values

我想出了对现有代码的以下修改,但它无法正常工作!我在% Distribution列中只有零。

USE MyDatabase

SELECT [Room Nights],
       COUNT([Room Nights]) AS 'Count of RN',
       CAST(COUNT([Room Nights])/(SELECT COUNT([Room Nights])*100. FROM HOLDINGS2) AS DECIMAL (9,0)) AS '% Distribution'
FROM HOLDINGS2
GROUP BY [Room Nights]

基本上,% Distribution列应取Count of RN并将其除以总计Count of RN

4 个答案:

答案 0 :(得分:5)

这样可行:

select [Room Nights],
  count([Room Nights]) AS 'Count of RN',
  cast(
    (count([Room Nights])
    /
    (Select Count([Room Nights]) * 1.0 from HOLDINGS2) 
   ) * 100 as decimal(6,1)
  ) as '% Distribution'    
FROM HOLDINGS2
GROUP BY [Room Nights]

子查询中的* 1.0强制浮点除法,外部强制转换限制精度。

或者,当您使用现代版本的MSSQL时,您可以使用窗口函数:

cast(count([Room Nights])/(sum(count([Room Nights])*1.0) over ()) * 100 as decimal(6,1))

答案 1 :(得分:1)

您可以使用窗口函数计算% Distribution,乘以100.0以强制结果为float,然后将逗号后的所有内容保留为1位数:

select [Room Nights]
      , count([Room Nights]) as [Count of RN]
      , cast(100.0 * count([Room Nights])/(sum(count([Room Nights])) over ()) as decimal(6,1)) as [% Distribution]
from HOLDINGS2
group by [Room Nights]   

SQLFiddle

您还可以使用子查询:

select [Room Nights]
     , count([Room Nights]) as [Count of RN]
     , cast(100.0 * count([Room Nights])/(select count([Room Nights]) from HOLDINGS2) as decimal(6,1)) as [% Distribution]
from HOLDINGS2
group by [Room Nights]  

SQLFiddle

答案 2 :(得分:1)

尝试:

DECLARE @t TABLE
    (
      [Room Nights] INT ,
      [Count of RN] INT
    )

INSERT  INTO @t
VALUES  ( 1, 3 ),
        ( 4, 10 ),
        ( 5, 6 ),
        ( 7, 25 )


SELECT  * ,
        ROUND([Count of RN] * 100.0
              / SUM([Count of RN]) OVER ( ORDER BY ( SELECT NULL ) ), 1) AS [Percent]
FROM    @t        

输出:

Room Nights Count of RN Percent
1           3           6.800000000000
4           10          22.700000000000
5           6           13.600000000000
7           25          56.800000000000

编辑:我错过了RN的数量是分组查询的结果。这是修改后的声明:

SELECT  [RN] ,
        COUNT(S) AS C ,
        CAST(COUNT(S) * 100.0 / SUM(COUNT(S)) OVER () AS DECIMAL(10, 1)) AS [Percent]
FROM    @t
GROUP BY [RN]   

答案 3 :(得分:0)

尝试这样的事情

select [Room Nights],
       count([Room Nights]) AS 'Count of RN',
       (CONVERT(DECIMAL(9,2),count([Room Nights]))/(Select Count([Room Nights]) from HOLDINGS2))*100 as '% Distribution'
FROM HOLDINGS2
GROUP BY [Room Nights]