select T.* from (
select KURL ,
Sortup=sum (case when (InitialPage>CurrentPage or
(InitialPage = CurrentPage and InitialRank > CurrentRank)) then 1 else 0 end),
Sortdown=sum (case when (InitialPage<CurrentPage or
(InitialPage = CurrentPage and InitialRank < CurrentRank)) then 1 else 0 end),
Thesame=sum(case when(InitialPage = CurrentPage and InitialRank=CurrentRank) then 1 else 0 end),
firstpage=sum(case when(CurrentPage=1) then 1 else 0 end),row_number() over(order by KURL asc) as RowNum
from Keywords
group by KURL)T
where RowNum between {0} and {1}
select KURL ,
Sortup=sum (case when (InitialPage>CurrentPage or
(InitialPage = CurrentPage and InitialRank > CurrentRank)) then 1 else 0 end),
Sortdown=sum (case when (InitialPage<CurrentPage or
(InitialPage = CurrentPage and InitialRank < CurrentRank)) then 1 else 0 end),
Thesame=sum(case when(InitialPage = CurrentPage and InitialRank=CurrentRank) then 1 else 0 end),
firstpage=sum(case when(CurrentPage=1) then 1 else 0 end),row_number() over(order by KURL asc) as RowNum
from Keywords
group by KURL)T
where RowNum between {0} and {1}