0

我运行上面的 sql 语句,我得到了这个。[IMG]http://i1093.photobucket.com/albums/i422/walkgirl_1993/asd-1_zps5506632e.jpg[/IMG] 我正在尝试显示最新的日期,你可以参见 3 和 4。对于 caseid 3,它应该显示最新的行,即 2012-12-20 16:12:36.000。我尝试使用 group by,order by。谷歌一些网站说使用排名,但我不确定排名,因为我没有真正获得排名。一些建议?

select [Case].CaseID, Agent.AgentName, Assignment.Description, A.AgentName as EditedBy, A.DateEdited from Agent inner join [Case-Agent] on [Case-Agent].AgentID = Agent.AgentID inner join [Assignment] on Assignment.AssignmentID = [Case-Agent].AssignmentID inner join [Case] on [Case].CaseID = [Case-Agent].CaseID inner join (select EditedCase.CaseID, [EditedCase].DateEdited, [Agent].AgentName from EditedCase inner join [Agent] on [Agent].AgentID = [EditedCase].AgentID) A on A.CaseID = [Case].CaseID where [Assignment].AssignmentID = 0 
4

2 回答 2

0

要使用 RANK 执行此操作,您只需将 RANK 添加到子查询并为每个 CaseID 和 Agent 对 DateEdited 进行排名,然后在主查询中放置一个 WHERE 子句以仅选择排名为 1 的行。我想我有得到正确的分区子句 - 没有看到你的数据有点难。

像这样:

SELECT 
  [Case].CaseID 
  ,Agent.AgentName 
  ,Assignment.Description 
  ,A.AgentName AS EditedBy 
  ,A.DateEdited 
FROM Agent 
  INNER JOIN [Case-Agent] ON [Case-Agent].AgentID = Agent.AgentID 
  INNER JOIN [Assignment] ON Assignment.AssignmentID = [Case-Agent].AssignmentID 
  INNER JOIN [Case] ON [Case].CaseID = [Case-Agent].CaseID 
  INNER JOIN (SELECT 
                EditedCase.CaseID 
                ,[EditedCase].DateEdited 
                ,[Agent].AgentName
                ,RANK ( ) OVER (PARTITION BY EditedCase.CaseID, [Agent].AgentName 
                               ORDER BY [EditedCase].DateEdited DESC ) AS pos
              FROM EditedCase 
                INNER JOIN [Agent] on [Agent].AgentID = [EditedCase].AgentID) A on A.CaseID = [Case].CaseID 
WHERE [Assignment].AssignmentID = 0 
  AND pos = 1

您还可以将子查询更改为带回 MAX 日期的聚合查询,如下所示:

SELECT 
  [Case].CaseID 
  ,Agent.AgentName 
  ,Assignment.Description 
  ,A.AgentName AS EditedBy 
  ,A.DateEdited 
FROM Agent 
  INNER JOIN [Case-Agent] ON [Case-Agent].AgentID = Agent.AgentID 
  INNER JOIN [Assignment] ON Assignment.AssignmentID = [Case-Agent].AssignmentID 
  INNER JOIN [Case] ON [Case].CaseID = [Case-Agent].CaseID 
  INNER JOIN (SELECT 
                EditedCase.CaseID 
                ,MAX([EditedCase].DateEdited) AS DateEdited 
                ,[Agent].AgentName
              FROM EditedCase 
                INNER JOIN [Agent] on [Agent].AgentID = [EditedCase].AgentID
              GROUP BY
                EditedCase.CaseID
                ,[Agent].AgentName) A on A.CaseID = [Case].CaseID 
WHERE [Assignment].AssignmentID = 0 
  AND pos = 1
于 2012-12-20T09:17:46.140 回答
0

你走在正确的轨道上;您需要在这里使用排名功能,例如row_number()

with LatestCase as
(
  select [Case].CaseID
    , Agent.AgentName
    , Assignment.Description
    , A.AgentName as EditedBy
    , A.DateEdited
    , caseRank = row_number() over (partition by [Case].CaseID order by A.DateEdited desc)
  from Agent
    inner join [Case-Agent] on [Case-Agent].AgentID = Agent.AgentID
    inner join [Assignment] on Assignment.AssignmentID = [Case-Agent].AssignmentID
    inner join [Case] on [Case].CaseID = [Case-Agent].CaseID
    inner join
    (
      select EditedCase.CaseID
        , [EditedCase].DateEdited
        , [Agent].AgentName
      from EditedCase
        inner join [Agent] on [Agent].AgentID = [EditedCase].AgentID
    ) A on A.CaseID = [Case].CaseID where [Assignment].AssignmentID = 0
)
select *
from LatestCase
where caseRank = 1
于 2012-12-20T09:20:05.903 回答