匹配两个字段以获得组中的最高计数

时间:2019-04-05 16:54:45

标签: group-by count

我需要确定每个SupportGroupName分组中最高AgentCount的两个字段(SupportGroupName和PMgr)是否匹配。

这是到目前为止我的代码输出的结果,但是我不知道如何编写要实现的代码:

+------------------+---------------------+------------+---------------+
| SupportGroupName | SupportGroupManager | AgentCount |     PMgr      |
+------------------+---------------------+------------+---------------+
| AE DS            | Sarah Smith         |         16 | Honey Roth    |
| AE DS            | Sarah Smith         |          5 | Sarah Smith   |
| AE DS            | Sarah Smith         |          1 | Starr Gilley  |
| AEOpenstack      | Ron Jerrold         |          1 | Chris Lattel  |
| Agent 360        | Keith Barrone       |          1 | Brian Terrell |
| Agent 360        | Keith Barrone       |          1 | Kari Sampson  |
| Agent 360        | Keith Barrone       |          3 | Keith Barrone |
| Agent 360        | Keith Barrone       |          2 | Mike Randall  |
+------------------+---------------------+------------+---------------+

我可以编写一个CASE语句来比较这两个字段,但不能只比较每个组中最高的座席人数。

SELECT SG.SupportGroupName
     , SG.SupportGroupManager
     , Count(SGM.SupportGroupAgent) AS AgentCount
     , P.ManagerName AS PMgr
FROM ARSystem_Metrics.dbo.BI_CTM_SupportGroup SG
LEFT OUTER JOIN ARSystem_Metrics.dbo.BI_CTM_SupportGroupsAndMembers SGM 
  on SG.SupportGroupId = SGM.SupportGroupId
LEFT JOIN ARSystem_Metrics.dbo.BI_CTM_People P 
  ON SGM.SupportGroupAgentCorpId = P.CorporateId
WHERE SG.status = 'Enabled' 
  and SGM.SupportGroupAssociationId = 'Member' 
  AND P.status = 'Enabled'
GROUP BY SG.SupportGroupName, SG.SupportGroupManager, P.ManagerName

评论的预期结果:

+-------------------+---------------------+------------+---------------+---------+
|  SupportGroupName | SupportGroupManager | AgentCount |     PMgr      | Match?  |
+-------------------+---------------------+------------+---------------+---------+
| AE DS             | Sarah Smith         |         16 | Honey Roth    | No      |
| AEOpenstack       | Ron Jerrold         |          1 | Chris Lattel  | No      |
| Agent 360         | Keith Barrone       |         3  | Keith Barrone | Yes     |
+-------------------+---------------------+------------+---------------+---------+

0 个答案:

没有答案