1

我整天都在拉头发,谁能帮帮我。我认为这可能很简单,但我不能完全得到我想要的。

表中的数据如下所示

CallId      ProblemDescription  CurrentAction     CurrentEngineer    EventNumber
CON-00002                       Respond to Call                      0
WHS-00001                       Respond to Call                      0
WHS-00002                       Respond to Call                      0
WHS-00003                       Respond to Call                      0
WHS-00003                       Fix Problem                          1
WHS-00003                       Problem Fixed                        2

我想要做的是显示具有最大事件数的每个 CallId 的所有列。IE

CallId      ProblemDescription  CurrentAction     CurrentEngineer    EventNumber
CON-00002                       Respond to Call                      0
WHS-00001                       Respond to Call                      0
WHS-00002                       Respond to Call                      0
WHS-00003                       Problem Fixed                        2

但是我不知道该怎么做。

目前我正在使用

Select CallId, MAX(ProblemDescription), MAX(CurrentAction), MAX(CurrentEngineer),
MAX(EventNumber) FROM [Table] GROUP BY CallId

这显然如何单独返回每个列的最大值。所以在上面的情况下 CurrentAction 列显示Respond to Call不是Respond to Call

谁能告诉我如何做到这一点???

谢谢

4

3 回答 3

3

我假设您正在寻找DENSE_RANK

WITH CTE AS
(
   SELECT 
      CallId, 
      ProblemDescription, 
      CurrentAction, 
      CurrentEngineer, 
      EventNumber,
      Rank = DENSE_RANK() OVER (PARTITION BY CallId ORDER BY EventNumber DESC)
   FROM dbo.TableName
)
SELECT 
   CallId, ProblemDescription, CurrentAction, CurrentEngineer, EventNumber
FROM 
   CTE
WHERE 
   RANK = 1

如果您只想要一行,CallId即使有多个具有相同的最大EventNumber使用量ROW_NUMBER

于 2013-03-19T15:56:40.060 回答
1

您想为此使用 row_number() :

select CallId, ProblemDescription, CurrentAction, CurrentEngineer, EventNumber
from (select t.*,
             ROW_NUMBER() over (partition by callId order by eventnumber desc) as seqnum
      from t
     ) t
where seqnum = 1
于 2013-03-19T15:54:45.850 回答
0

使用此代码。* 仅在事件编号列上使用最大聚合。

SELECT CallID, Problem_Description, Current_Action, Current_Engineer, MAX( Event_Number ) FROM test.data GROUP BY CallID;

这可能会帮助你

于 2013-03-19T16:20:28.143 回答