2014-01-08 105 views
0

我正在處理查詢,但StoryNumber以多行形式返回。在特定列中組合具有相同值的行

下面是該查詢:

SELECT 
    STY.[Number] StoryNumber 
    ,ISNULL(SUM(DTH.[Hours]), 0) AS WorkHours 
    ,MAX(CASE 
      WHEN DTH.Hours != 0 
       THEN DTH.ActivityDate 
      END) AS LatestActivityDate 
FROM Task TSK 
LEFT JOIN DailyTaskHours DTH ON TSK.PK_Task = DTH.TaskId 
LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story 
LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint 
LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product 
GROUP BY LEN(STY.[Number]) 
    ,STY.[Number] 
    ,TSK.[Name] 

該查詢實際上是簡化的,所有的連接條件是必需的。

這是上面查詢的輸出:

enter image description here

我怎樣才能像結合StoryNumber的,當有多個如OHC-1和workhours並找到最高activitydate?從GROUP BY

回答

3

刪除多餘的字段:

SELECT STY.[Number] StoryNumber 
     ,ISNULL(SUM(DTH.[Hours]), 0) AS WorkHours 
     ,MAX(CASE WHEN DTH.Hours != 0 
       THEN DTH.ActivityDate 
      END) AS LatestActivityDate 
FROM Task TSK 
LEFT JOIN DailyTaskHours DTH ON TSK.PK_Task = DTH.TaskId 
LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story 
LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint 
LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product 
LEFT JOIN [User] USR ON TSK.PointPerson = USR.DisplayName 
GROUP BY STY.[Number] 
相關問題