0

我让我的 SQL 代码返回 Max t.[CreatedDateTime] (见下文)给我正确的结果,但是我希望添加一个额外的列 t.[Owner] 并且不在结果中显示 [CreatedDateTime] 但仍然基于Max([t.CreatedDateTime]) 上的结果

SELECT i.[RecID] as AssigneeID, max(t.[CreatedDateTime]) CreatedDateTime
FROM Incident as i
Join Task as t On i.[RecID] =  t.[ParentLink_RecID] 
WHERE i.[Status] <> 'Closed'
AND t.[OwnerTeam] IS NOT NULL
group by i.[RecID]
Order By i.[RecID] DESC

当前结果

AssigneeID              CreatedDateTime 
FFFE53B2305D47388937D1F5B6722DF3    2013-05-13 14:36:55.000
FFD64AD977A449258A88B9A404795631    2013-05-21 14:11:28.000
FFD557F622A34FD1A386B427B66ADAFC    2013-04-26 12:32:05.000

所需结果

AssigneeID                          Owner
FFFE53B2305D47388937D1F5B6722DF3    Bob
FFD64AD977A449258A88B9A404795631    Fred
FFD557F622A34FD1A386B427B66ADAFC    Harry

提前感谢您的任何帮助..

4

4 回答 4

0

您可以将当前查询结果与记录 ID 列上的任务表(获取所有者列数据)连接起来。例如:

select t1.AssigneeID, t2.Owner from (your current query) t1, Task t2 where t1.AssigneeID = t2.ParentLink_RecID
order by 1 desc

完整的前:

select t1.[AssigneeID] as AssigneeID, t2.[Owner] as Owner 
from Task t2 
LEFT JOIN (SELECT I. [RecID] as AssigneeID, max(t.[CreatedDateTime]) CreatedDateTime FROM Incident as i Join Task as t On i.[RecID] = t.[ParentLink_RecID] WHERE i.[Status] <> 'Closed' AND t.[OwnerTeam] IS NOT NULL group by i.[RecID]) t1 
on t2.[ParentLink_RecID] = t1.[AssigneeID] 
order by t1.[AssigneeID] desc
于 2013-06-16T22:18:29.047 回答
0

尝试这个

SELECT x.AssigneeID, tt.Owner
FROM (
   SELECT i.[RecID] as AssigneeID, max(t.[CreatedDateTime]) CreatedDateTime
   FROM Incident as i
   Join Task as t On i.[RecID] =  t.[ParentLink_RecID] 
   WHERE i.[Status] <> 'Closed'
   AND t.[OwnerTeam] IS NOT NULL
   group by i.[RecID]
) x
JOIN Task as TT ON (x.RecID = tt.ParentLine_RecID)
ORDER BY x.AssigneeID DESC
于 2013-06-17T04:11:49.413 回答
0

尝试:

SELECT AssignedID, Owner
FROM
( 
 SELECT i.[RecID] as AssigneeID, max(t.[CreatedDateTime]) CreatedDateTime, t.[Owner]
FROM Incident as i
Join Task as t On i.[RecID] =  t.[ParentLink_RecID] 
WHERE i.[Status] <> 'Closed'
AND t.[OwnerTeam] IS NOT NULL
group by i.[RecID]
) S
Order By AssignedID DESC
于 2013-06-16T22:29:09.777 回答
0

您没有说,但根据 [columnName] 语法,看起来您正在使用 MS SQLServer。如果是这样,那么您可以使用 RANK() 或 ROW_NUMBER() 来获得有效的解决方案。

with rankedResult as (
  select i.[AssigneeID],
         t.[Owner],
         row_number() over( partition by i.[AssigneeID] 
                            order by t.[CreatedDateTime] desc, t.[Owner] ) seq
    from Incident i
    join Task t
      on t.[ParentLink_RecID] = i.[RecID]
   where i.[Status] <> 'Closed'
     and t.[OwnerTeam] is not null
)
select [AssigneeID],
       [Owner]
  from rankedResult
 where seq=1
;
于 2013-06-17T01:47:28.157 回答