SQL Server:在ROW_NUMBER OVER PARTITION BY上遇到问题

时间:2016-04-25 03:56:54

标签: sql-server

我在计算sql语句中row_number的最大值时遇到了麻烦。

我查询搜索OriginalDocumentNumber,但数据与查询全部不一样 http://i.stack.imgur.com/xwVN9.png

select 
    OriginalDocumentNumber, FilterRound, p2_Reason, rowno, p2_FinalGrade
from 
    (select  * ,row_number() over (partition by OriginalDocumentNumber,FilterRound order by p2_FinalGrade desc) rowno 
                from #TempDa 
            ) l  
        where  
        l.p2_FinalGrade in ('d','f')  
         and l.rowno = 1
         and l.OriginalDocumentNumber='590200054'
         and l.OriginalDocumentNumber+l.FilterRound not in (select OriginalDocumentNumber+FilterRound from #TempDa where p2_FinalGrade in ('a','b','c')) 

查询2输出image query 2

select OriginalDocumentNumber ,FilterRound ,  p2_Reason , rowno , p2_FinalGrade
            from (
                    select * ,row_number() over (partition by OriginalDocumentNumber,FilterRound order by p2_FinalGrade desc) rowno 
                    from #TempDa 
                ) l  
            where  
            l.p2_FinalGrade in ('d','f')  
             and l.rowno = 1
             -- and l.OriginalDocumentNumber='590200054'
             and l.OriginalDocumentNumber+l.FilterRound not in (select OriginalDocumentNumber+FilterRound from #TempDa where p2_FinalGrade in ('a','b','c')) 

0 个答案:

没有答案