SQL 对计数列求和以计算百分比分布的语法
SQL syntax to SUM a Count column for calculating percentage distribution
我正在使用 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
。
您可以使用 window 函数计算 % 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]
你也可以使用子查询:
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]
尝试这样的事情
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]
这可行:
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 时,您可以使用 window 函数:
cast(count([Room Nights])/(sum(count([Room Nights])*1.0) over ()) * 100 as decimal(6,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]
我正在使用 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
。
您可以使用 window 函数计算 % 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]
你也可以使用子查询:
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]
尝试这样的事情
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]
这可行:
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 时,您可以使用 window 函数:
cast(count([Room Nights])/(sum(count([Room Nights])*1.0) over ()) * 100 as decimal(6,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]