根据版本号查询行

时间:2011-11-03 17:17:37

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

我有一个返回以下结果的查询:

6464EFD1-413B-4AE2-811D-700A035BF3DB    111102ABF00002D scott       butty       2
DC37D185-0A47-4E19-8FEC-A86428987418    111102ABF00005D NotActive   Attwell     1
0F293E29-5E5B-4C0D-BC25-D05CDB5D6709    111102ABF00002D scott       butty       1
GUID                                    Worksheet       Name              Version

最后一列是版本号。我只想要返回最新的版本 - 在这种情况下,底部的行将被省略,只有前两行将被拉出。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

SELECT *
FROM MyTable A
WHERE A.Version = (SELECT MAX(Version) AS Version FROM MyTable B WHERE B.Worksheet = A.Worksheet)

答案 1 :(得分:1)

select *
from
(
    select *,
        row_number() over(partition by worksheet order by version desc) as rownum
    from yourTable
) a
where rownum = 1