如何计算百分比?

时间:2015-01-14 09:13:32

标签: sql sql-server percentage

下面给出的是数据库表(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

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

这里Count1 3是greates值。基于此我必须找到Count1值的百分比。在Count0 2中是最大的值。基于此我必须找到Count0值的百分比。

期待看跌:

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

帮我找到合适的解决方案。谢谢。

3 个答案:

答案 0 :(得分:2)

样本表

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获取最大值并计算该

上的百分比计算

<强> QUERY

 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
  • 查看工作查询HERE

答案 1 :(得分:1)

试试这个:

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级别添加%符号,以便百分比列可以保留为数字而不是文本。此外,您可能希望将初始查询的结果存储在表变量中,然后从中读取,而不是将整个查询包含在子查询中。

Demo

答案 2 :(得分:0)

您的SELECT语句应具有结构

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

百分比计算总是

Num * 100.0 / Total