如何计算百分比?

How to calculate percentage?

下面给出的是数据库table(TableA)。

SlNo  TeamName  WorkInfo 
----  -------   -------  
1     Team1        0    
2     Team1        0    
3     Team1        1     
4     Team2        1    
5     Team2        1 
6     Team2        1      
7     Team3        0      
8     Team3        1      

我有两个用于选择开始日期和结束日期的文本框。我想显示这些日期之间的详细信息。

下面的查询给出了基于开始日期和结束日期的详细信息和计数。

SELECT TeamName,Count(DISTINCT CASE WHEN WorkInfo = 1 THEN SlNo end) AS Count1 , 
       Count(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) AS Count0 FROM tableA 
WHERE (SubmitDate BETWEEN @start AND @end) GROUP BY TeamName

上面的查询显示如下所示的输出:

TeamName   Count1   Count0
--------   -----    ------
Team1        1        2
Team2        3        0
Team3        1        1

我想根据 Count1Count0 中的 greatest count 显示百分比。帮我修改上面的解决方案。 The expecting output sample is given below:

这里 Count1 3 是最大的 value.Based 我必须找到 Count1 值的百分比。在 Count0 中,2 是最大的 value.Based,我必须找到 Count0 值的百分比。

期待输出:

TeamName   Count1   Count0  Percentage1  Percentage0
--------   -----    ------  -----------  -----------
Team1        1        2        33.33%         100%
Team2        3        0        100%           0%
Team3        1        1        33.33%         50%  

帮我找到合适的解决办法。谢谢。

您的 SELECT 语句应具有结构

SELECT Count_Expr, Count2_EXPR, PCT_EXPR, PCT2_EXPR FROM ...

百分比计算总是

Num * 100.0 / Total

试试这个:

declare @maxcount1 float
declare @maxcount0 float

select @maxcount1 = max(count1), @maxcount0 = max(count0)
from 
(SELECT TeamName,Count(DISTINCT CASE WHEN WorkInfo = 1 THEN SlNo end) AS Count1 , 
 Count(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) AS Count0 FROM tableA 
 WHERE (SubmitDate BETWEEN @start AND @end) GROUP BY TeamName) as s

select teamname, count1, count0, 
round((cast((count1 * 100) as float)/@maxcount1),2) as percentage1,
round((cast((count0 * 100) as float)/@maxcount0),2) as percentage0
from 
(SELECT TeamName,Count(DISTINCT CASE WHEN WorkInfo = 1 THEN SlNo end) AS Count1 , 
 Count(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) AS Count0 FROM tableA 
 WHERE (SubmitDate BETWEEN @start AND @end) GROUP BY TeamName) as s

请注意,您应该尝试进行格式设置,例如在 UI 级别添加 % 符号,以便百分比列可以保留为数字而不是文本。此外,您可能希望将初始查询的结果存储在 table 变量中,然后从中读取,而不是将整个查询包含在子查询中。

Demo

样本TABLE

CREATE TABLE #TEMP(TeamName VARCHAR(20),Count1 NUMERIC(10,2),Count0 NUMERIC(10,2)) 

INSERT INTO #TEMP
SELECT 'Team1' , 1 ,2 
UNION ALL
SELECT 'Team2', 3,0
UNION ALL 
SELECT 'Team3', 1,1

使用 PARTITION BY 获取最大值并计算其百分比

查询

 SELECT *
,CAST(CAST(Count1 * 100 /CAST((MAX(Count1) OVER())AS NUMERIC(4,2)) AS NUMERIC(38,2)) AS VARCHAR(6))+'%' PERCENTAGE1
,CAST(CAST(Count0 * 100 /CAST((MAX(Count0) OVER())AS NUMERIC(4,2)) AS NUMERIC(38,2)) AS VARCHAR(6))+'%' PERCENTAGE0
FROM TEMP

更新

如果你想从你的查询中计算百分比,你可以使用下面的代码

;WITH CTE AS
(
    SELECT TeamName,Count(DISTINCT CASE WHEN WorkInfo = 1 THEN SlNo end) AS Count1 , 
           Count(DISTINCT CASE WHEN WorkInfo = 0 THEN SlNo end) AS Count0 FROM tableA 
    WHERE (SubmitDate BETWEEN @start AND @end) GROUP BY TeamName 
)
SELECT *
,CAST(CAST(Count1 * 100 /CAST((MAX(Count1) OVER())AS NUMERIC(4,2)) AS NUMERIC(38,2)) AS VARCHAR(6))+'%' PERCENTAGE1
,CAST(CAST(Count0 * 100 /CAST((MAX(Count0) OVER())AS NUMERIC(4,2)) AS NUMERIC(38,2)) AS VARCHAR(6))+'%' PERCENTAGE0
FROM CTE
  • 查看工作查询[=​​13=]