views:

55

answers:

1

I want write a stored proc in T-SQL to return the top 5 most highly rated and the bottom 5 most lowly rated articles from an Articles table, determined by the 'rating' column.

I was thinking of using a union on two selects but I'm not sure how to write it.

+3  A: 
select * from (select top 5 *, 'Bottom Five' as Ranking from Call order by id ) a
union all
select * from (select top 5 *, 'Top Five' as Ranking from Call order by id desc ) b
RedFilter
+1 Faster than me.
Joe Stefanelli
Thanks for the prompt reply. I keep getting 'Incorrect syntax near the keyword 'union' ' errors for the query above. Should I move the order by to the end?
Zener Diode
@Zener: see my update
RedFilter
Nice one @RedFilter. I was scratching around with lots of temporary tables, this is much neater. If I want the MOST RECENT highest and lowest ranking can I just add a Date DESC on the second orderby only?
Zener Diode