以最简单的方式说明:
表用户: (没什么特别的)
-----------------------------------------
UserID | Fullname
-----------------------------------------
User1 | JohnDoe
-----------------------------------------
User2 | PeterPan
-----------------------------------------
User3 | BatMan
-----------------------------------------
表组
----------------------------------------
GroupID | Group_Name |
----------------------------------------
1 | Manager |
----------------------------------------
2 | Reviewer |
----------------------------------------
表员工
----------------------------------------
UserID | GroupID |
----------------------------------------
User1 | 1 |
----------------------------------------
User2 | 2 |
----------------------------------------
User3 | 2 |
----------------------------------------
表项目分配
--------------------------------------------------------
ID | ProjectID | Reviewer | Status
--------------------------------------------------------
1 | 1 | User2 | Approved
--------------------------------------------------------
2 | 2 | User2 | Disapproved
---------------------------------------------------------
3 | 2 | User2 | Approved
---------------------------------------------------------
4 | 3 | User3 | Disapproved
---------------------------------------------------------
5 | 3 | User3 | Disapproved
---------------------------------------------------------
6 | 3 | User3 | Disapproved
---------------------------------------------------------
7 | 3 | User3 | Approved
---------------------------------------------------------
数据简要详情:
问题:
我想查询项目分配表,以获得每个审核人
已批准(状态)项目组的百分比 现在,我得到的是所有项目的总数,而非每个评审员指定的项目
我的Sql查询:
SELECT `Reviewer`,
`ProjectID`,
COUNT(*) as NumberOfTimesProjectSubmitted ,
Round((Select COUNT(*) From `Project Assignment`
Where Status='Approved') / (COUNT(*)) *100,2) AS Approval_Percentage
FROM `Project Assignment`
GROUP by ProjectID
预期查询结果可能是这样的:
-----------------------------------------------------------------------
Reviewer | ProjectID | NumberOfTimesProjectSubmitted | Approval Percentage
-----------------------------------------------------------------------
User2 | 1 | 1 | 100.00
-----------------------------------------------------------------------
User2 | 2 | 2 | 50.00
-----------------------------------------------------------------------
User3 | 3 | 4 | 25.00
------------------------------------------------------------------------
很抱歉,如果我错过了一些细节,那就是我如何解释我要解决的问题。我确实尝试过几次(已经差不多一天了)但是没有得到结果。我尝试了单独的查询,但仍然在Reviewer下获得了不是每个项目的项目总数。
答案 0 :(得分:1)
在最简单的级别,您只需按评论者和项目ID进行分组。但是,您也可以删除子查询以获得批准百分比。
SELECT `Reviewer`,
`ProjectID`,
COUNT(*) as NumberOfTimesProjectSubmitted ,
Round((SUM(CASE When Status = 'Approved' THEN 1 ELSE 0 END) / COUNT(*), 2))
AS Approval_Percentage
FROM `Project Assignment`
GROUP by Reviewer, ProjectID
答案 1 :(得分:1)