2012-12-20 129 views
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。谷歌一些網站說,使用排名,但我不知道排名,因爲我真的沒有排名。一些建議?從一個ID獲取最新日期

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 

回答

0

要做到這一點使用RANK,你只需要在RANK添加到子查詢,並獲得排名DateEdited每個CaseID和代理,然後在主查詢放在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 
+0

非常感謝!它實際上有效!但我仍然沒有得到排名的想法 – BunnyRiven

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 
相關問題