查询以选择最新更新的记录

时间:2013-11-26 07:26:34

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2

我正在使用SQL Server 2008,我正在使用以下查询来检索记录,

SELECT 
    Var_AssoId, Var_Geo,
    Var_Vertical, Var_AccountID,               
    Dt_VisaValidFrom, Dt_VisaValidTill,
    Var_Grade, Var_ProjectID, Bit_SupervisorResponse,
    a.Int_CommentID, Var_CommentsEntered,Dt_Date, 
    Bit_MailUploadStatus, Var_MailUploadPath,  
    a.Dt_UpdatedOn, Var_UpdatedBy, b.Var_SupervisorComments  
FROM 
    Testingpmo_Travelready_SupervisorInput a   
INNER JOIN 
    Testingpmo_Travelready_SupervisorComments b ON a.Int_CommentID = b.Int_CommentID  
ORDER BY 
    a.Dt_UpdatedOn 

以下是此查询的输出

Var_AssoId    Int_CommentID          Dt_UpdatedOn   
 251922     2       9/25/13 5:22 PM 
 305561     2       9/25/13 5:24 PM 
 109483     1       9/25/13 5:24 PM 
 305561     4       9/25/13 6:09 PM 
 109483     3       10/1/13 12:44 PM    
 109483     3       10/1/13 12:47 PM    
 109483     3       10/1/13 12:48 PM    
 109483     3       10/1/13 12:51 PM    
 109483     3       10/1/13 2:23 PM 

我想在每个Var_AssoId中只获得一条最新的更新记录。

例如,查询的输出应该是,

 Var_AssoId Int_CommentID   Dt_UpdatedOn    
 251922     2       9/25/13 5:22 PM 
 305561     4       9/25/13 6:09 PM 
 109483     3       10/1/13 2:23 PM 

获取此输出需要在查询中添加的内容吗?

3 个答案:

答案 0 :(得分:0)

查询:

SELECT a.Var_AssoId,
           a.Var_Geo,
           a.Var_Vertical,
           a.Var_AccountID,
           a.Dt_VisaValidFrom,
           a.Dt_VisaValidTill,
           a.Var_Grade,
           a.Var_ProjectID,
           a.Bit_SupervisorResponse,
           a.Int_CommentID,
           a.Var_CommentsEntered,
           a.Dt_Date,
           a.Bit_MailUploadStatus,
           a.Var_MailUploadPath,
           a.Dt_UpdatedOn,
           a.Var_UpdatedBy,
           a.Var_SupervisorComments
FROM (
        SELECT Var_AssoId,
           Var_Geo,
           Var_Vertical,
           Var_AccountID,
           Dt_VisaValidFrom,
           Dt_VisaValidTill,
           Var_Grade,
           Var_ProjectID,
           Bit_SupervisorResponse,
           a.Int_CommentID,
           Var_CommentsEntered,
           Dt_Date,
           Bit_MailUploadStatus,
           Var_MailUploadPath,
           a.Dt_UpdatedOn,
           Var_UpdatedBy,
           b.Var_SupervisorComments,
           ROW_NUMBER()OVER(PARTITION BY Var_AssoId ORDER BY a.Dt_UpdatedOn Desc ) rnk
    FROM Testingpmo_Travelready_SupervisorInput a
    INNER JOIN Testingpmo_Travelready_SupervisorComments b ON a.Int_CommentID=b.Int_CommentID
)a
WHERE a.rnk = 1
    ORDER BY a.Dt_UpdatedOn

答案 1 :(得分:0)

假设您的表结构为:

create table Testingpmo_Travelready_SupervisorInput(Var_AssoId int,
       Var_Geo int,
       Var_Vertical int,
       Var_AccountID int,               
       Dt_VisaValidFrom datetime,
       Dt_VisaValidTill datetime,
       Var_Grade char(1),
       Var_ProjectID int,
       Bit_SupervisorResponse bit,
       Int_CommentID int,
       Var_CommentsEntered varchar(10),
       Dt_Date datetime,
       Bit_MailUploadStatus bit,
       Var_MailUploadPath varchar(10),
       Dt_UpdatedOn datetime,
       Var_UpdatedBy varchar(10));

create table Testingpmo_Travelready_SupervisorComments( Var_SupervisorComments  varchar(10),Int_CommentID int)

您可以将查询编写为:

SELECT a.Var_AssoId,
Var_Geo,
Var_Vertical,
Var_AccountID,               
Dt_VisaValidFrom,
Dt_VisaValidTill,
Var_Grade,
Var_ProjectID,
Bit_SupervisorResponse,
a.Int_CommentID,
Var_CommentsEntered,
Dt_Date,
Bit_MailUploadStatus,
Var_MailUploadPath,
a.Dt_UpdatedOn
Var_UpdatedBy,
b.Var_SupervisorComments            
FROM Testingpmo_Travelready_SupervisorInput a   
inner join  Testingpmo_Travelready_SupervisorComments b   
on a.Int_CommentID=b.Int_CommentID 
inner join  
(select t1.Var_AssoId,MAX(t1.Dt_UpdatedOn) as Dt_UpdatedOn
FROM Testingpmo_Travelready_SupervisorInput T1 
group by t1.Var_AssoId) T on a.Var_AssoId = T.Var_AssoId and a.Dt_UpdatedOn = T.Dt_UpdatedOn 
ORDER BY a.Dt_UpdatedOn 

答案 2 :(得分:0)

请尝试以下查询

SELECT 
    Var_AssoId, Var_Geo,
    Var_Vertical, Var_AccountID,               
    Dt_VisaValidFrom, Dt_VisaValidTill,
    Var_Grade, Var_ProjectID, Bit_SupervisorResponse,
    a.Int_CommentID, Var_CommentsEntered,Dt_Date, 
    Bit_MailUploadStatus, Var_MailUploadPath,  
    a.Dt_UpdatedOn, Var_UpdatedBy, b.Var_SupervisorComments  
FROM Testingpmo_Travelready_SupervisorInput a   
INNER JOIN Testingpmo_Travelready_SupervisorComments b ON a.Int_CommentID = b.Int_CommentID  
WHERE a.Dt_UpdatedOn = (SELECT MAX(sub.Dt_UpdatedOn) FROM Testingpmo_Travelready_SupervisorInput sub WHERE sub.Var_AssoId = Var_AssoId)
ORDER BY a.Dt_UpdatedOn