按查询分组不会每次都生成正确的结果

时间:2018-10-18 23:56:57

标签: sql sql-server group-by

我希望对一些我一直苦苦挣扎的SQL有所帮助。下面有两个示例,一个产生正确的结果,一个没有。我知道为什么,但是我只是不知道如何解决它。

我当前拥有的SQL是:

 SELECT COUNT(DISTINCT ea.PersonId) Allocated, 
                CASE WHEN MAX(ea.EngagementAssignmentStatusId) = 2 THEN COUNT(DISTINCT ea.PersonId) ELSE 0 END Confirmed,
                ea.EngagementLevelID FROM EngagementAssignment ea 
    INNER JOIN EngagementLevel el ON ea.EngagementLevelId = el.EngagementLevelID
    WHERE el.JobId = 12906 AND 
    ea.EngagementLevelID = 105901 AND
    el.IsActive = 1 AND 
    ea.IsActive = 1 AND 
    ea.Isassigned = 1
    GROUP BY ea.EngagementLevelID

Project_Id 12906数据

lastname    firstname   PersonId    EngagementAssignmentStatusId
Person 1    Person 1    9233            2
Person 2    Person 2    9664            2
Person 3    Person 3    9508            2
Person 4    Person 4    7602            2
Person 5    Person 5    6216            2
Person 6    Person 6    6672            2
Person 7    Person 7    8798            2
Person 8    Person 8    9424            2
Person 9    Person 9    8862            2
Person 10   Person 10   9938            1
Person 11   Person 11   9754            1
Person 12   Person 12   9659            1
Person 13   Person 13   7441            2
Person 14   Person 14   8532            2
Person 15   Person 15   7106            2

Project_Id 6756数据

lastname    firstname   PersonId    EngagementAssignmentStatusId
Person 1    Person 1    5045            1
Person 1    Person 1    5045            1
Person 2    Person 2    4583            1
Person 2    Person 2    4583            1
Person 2    Person 2    4583            1
Person 2    Person 2    4583            1
Person 2    Person 2    4583            1
Person 2    Person 2    4583            1
Person 2    Person 2    4583            1
Person 2    Person 2    4583            1
Person 3    Person 3    4584            1
Person 3    Person 3    4584            1
Person 3    Person 3    4584            1
Person 3    Person 3    4584            1
Person 3    Person 3    4584            1
Person 4    Person 4    7192            1
Person 4    Person 4    7192            1
Person 4    Person 4    7192            1
Person 4    Person 4    7192            1
Person 4    Person 4    7192            1
Person 4    Person 4    7192            1
Person 4    Person 4    7192            1
Person 4    Person 4    7192            1
Person 4    Person 4    7192            1
Person 5    Person 5    6939            1
Person 5    Person 5    6939            1
Person 5    Person 5    6939            1
Person 6    Person 6    4802            1
Person 7    Person 7    5074            1
Person 7    Person 7    5074            1
Person 7    Person 7    5074            1
Person 7    Person 7    5074            1
Person 7    Person 7    5074            1
Person 7    Person 7    5074            1

我想要得到的是人数

已分配, 确认(EngagementAssignmentStatusId = 2) 为EngagementLevelId,因此为分组依据。

2个大数据集是2个项目的基础数据。 max()语句为项目105901产生错误结果的情况。

“确认”的结果应为12,因为15条记录中有12条具有EngagementAssignmentStatusId = 2。

有人可以帮忙吗?

谢谢

1 个答案:

答案 0 :(得分:1)

“条件聚合”是通过在聚合函数中放置一个case表达式而形成的。

SELECT
    COUNT(DISTINCT ea.PersonId) Allocated
  , COUNT(DISTINCT CASE WHEN ea.EngagementAssignmentStatusId = 2 THEN ea.PersonId END) Confirmed
  , ea.EngagementLevelID
FROM EngagementAssignment ea
INNER JOIN EngagementLevel el ON ea.EngagementLevelId = el.EngagementLevelID
WHERE el.JobId = 12906
AND ea.EngagementLevelID = 105901
AND el.IsActive = 1
AND ea.IsActive = 1
AND ea.Isassigned = 1
GROUP BY
    ea.EngagementLevelID