如何在SQL中获取最新的更新记录?

时间:2015-02-10 05:25:14

标签: sql sql-server sql-server-2008

我已经编写了查询,但我没有收到最新的更新数据。我的意思是我有psn更新列,我想要最新更新日期的当前位置数据,根据“latestpsnupdate”我需要当前位置。

SELECT 
     k.KeyWord,
     pa.UserName,
     pa.ProjId,
     k.KeywordID,
     kp.currentposition,
     kp.PsnUpdateDate
FROM Tbl_ProjAssignment pa 
INNER JOIN Tbl_keywords k on k.KeywordID=pa.KeywordID 
INNER JOIN seo.Tbl_KeywordPosition kp 
ON kp.keywordid = pa.keywordid   
WHERE k.ProjId='105' ORDER BY PsnUpdateDate DESC

o / p

keyword        UserName ProjId   keywordid currentposition  PsnUpdateDa
ventis123       Tester    105     1453          6            2015-03-11 
ventis123       krishna   105     1453          6            2015-03-11
annuity         Tester    105     1449          8            2015-01-30 
annuity         Tester    105     1449          7            2015-01-27 
ventissitnev    Tester    105     1452          6            2015-01-16 
annuity         Tester    105     1449          55           2015-01-15 
annuity         Tester    105     1449          6            2015-01-08 
annuity         Tester    105     1449          10           2014-12-13 

o / p需要

期望o / p

keyword      UserName ProjId   keywordid currentposition  PsnUpdateDa
ventis123     Tester    105     1453        6              2015-03-11 
annuity       Tester    105     1449        8              2015-01-30
ventissitnev  Tester    105     1452        6              2015-01-16 
annuity       Tester    105     1449        55             2015-01-15

3 个答案:

答案 0 :(得分:0)

使用如下:

;WITH    CTE
AS ( SELECT k.KeyWord ,
            pa.UserName ,
            pa.ProjId ,
            k.KeywordID ,
            kp.currentposition ,
            kp.PsnUpdateDate ,
            ROW_NUMBER() OVER ( PARTITION BY k.keyword ORDER BY kp.PsnUpdateDate DESC ) AS positiondata
    FROM     Tbl_ProjAssignment pa
            INNER JOIN Tbl_keywords k ON k.KeywordID = pa.KeywordID
            INNER JOIN seo.Tbl_KeywordPosition kp ON kp.keywordid = pa.keywordid
    )

SELECT  *
FROM    CTE
WHERE   ProjId = '105'
        AND positiondata = 1

答案 1 :(得分:0)

试试这个,如果你要放GROUP BY,请确保取消组合字段在AGGREGATE。请参阅下面的答案:

SELECT 
     k.KeyWord,
     MAX(pa.UserName)UserName,
     pa.ProjId,
     k.KeywordID,
     kp.currentposition,
     MAX(kp.PsnUpdateDate)
FROM Tbl_ProjAssignment pa 
INNER JOIN Tbl_keywords k on k.KeywordID=pa.KeywordID 
INNER JOIN seo.Tbl_KeywordPosition kp 
ON kp.keywordid = pa.keywordid   
WHERE k.ProjId='105' 
GROUP BY k.keyword,pa.ProjId,k.KeywordID,kp.currentposition
ORDER BY kp.PsnUpdateDate DESC

答案 2 :(得分:0)

除了Row_number之外,我们也可以这样做..只是一个示例

    declare @t table (Keyword varchar(10), UserName  varchar(10),ProjId    int,keywordid int,perdate date   )
    insert into @t(Keyword,UserName,ProjId,keywordid,perdate)values ('ventis123','Tester',105,1453,'2015-03-11')
    insert into @t(Keyword,UserName,ProjId,keywordid,perdate)values ('ventis123','Tester',105,1453,'2015-03-11')
    insert into @t(Keyword,UserName,ProjId,keywordid,perdate)values ('annuity','Tester',105,1449,'2015-01-30')
    insert into @t(Keyword,UserName,ProjId,keywordid,perdate)values ('annuity','Tester',105,1449,'2015-01-27')

select DISTINCT tt.Keyword,
tt.UserName,
tt.ProjId,
t.Keyword,
t.perdate 
from  @t tt
    INNER JOIN 
         (SELECT MAX(keywordid)Keyword, 
                 MAX(perdate)perdate 
                 from @t 
             GROUP BY Keyword,UserName,ProjId )t
           ON t.Keyword = tt.keywordid  
          AND t.perdate = tt.perdate
          ORDER BY 1 desc