Matt Miller (#4) (12/17/2014)
I am feeling silly. There's a much simpler implementation, but I basically never use this function.The trick is to leverage the NTILE aggregate function rather than ROW_NUMBER. In which case the query would look a lot more like djj's.
with cte as
(
select *,
NTILE(4) over(PARTITION by MainGroup order by value DESC) as 'GroupN'
--the NTILE parameter divvies up the partitioned groups into 4 parts
--in this case NTILE #1 is the top 25% and NTILE 4 = bottom 25%
from #mytable
)
select * from cte
where GroupN=4
Maybe that was what I was remembering.