过滤最大值rn

时间:2011-03-15 09:08:13

标签: sql sql-server-2008 tsql sql-server-2005 window-functions

如何过滤此处的最大值

select  *
from    (
        select  *
        ,       row_number() over (partition by name order by ox asc) as rn
        from    YourTable
        ) as SubQueryAlias
where   rn = 1
在文件rn = 1

之上

我想过滤rn = max(rn)

2 个答案:

答案 0 :(得分:2)

order by ox asc更改为order by ox desc并使用rn = 1

select  *
from    (
        select  *
        ,       row_number() over (partition by name order by ox desc) as rn
        from    YourTable
        ) as SubQueryAlias
where   rn = 1

答案 1 :(得分:0)

选中此项以获得rn = max(rn):

select top 1 * from 
( select * , 
    row_number() over (partition by name order by ox asc) 
    as rn from YourTable ) 
as SubQueryAlias 
order by rn desc