我有以下数据库表(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
但我想根据Count1
和Count0
中的最大数量来显示百分比。
例如,在下面的预期输出中,3是最大Count1
值;基于此,我必须将计数值转换为此最大Count1
值的百分比。同样,2是最大Count0
值;基于此,我必须找到Count0
值的百分比。
TeamName Count1 Count0 Percentage1 Percentage0
-------- ----- ------ ----------- -----------
Team1 1 2 33.33% 100%
Team2 3 0 100% 0%
Team3 1 1 33.33% 50%
如何修改我的解决方案以产生预期的输出?
答案 0 :(得分:0)
SELECT TeamName, Count(TeamName) AS Count
FROM table
GROUP BY TeamName
您需要使用Count
功能 - 计算球队名称
您需要使用group by
来确定如何对计数进行分组
编辑答案。
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
GROUP BY TeamName