Top X records | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Top X records

I have a table containing some records group wise. A – 100 records
B – 100 records
C – 100 records
D – 100 records
E – 100 records I need to extract the top 50 record from each group in single query. Any idea or solution to extract. Thanks in advance. Regards
Rajneesh
please post your table structure
KH
How about this Select Top 50 * from Table where Fld =’A’
union
Select Top 50 * from Table where Fld =’B’
union
Select Top 50 * from Table where Fld =’C’
union
Select Top 50 * from Table where Fld =’D’ —————————————-
Contributing Editor, Writer & Forums Moderator
http://www.SQL-Server-Performance.Com Visit my Blog at
http://dineshasanka.spaces.live.com/

consider this approach: select * from maintable t
where col1 in(
select top 50 col1 from maintable t1
where t.col2=t1.col2)

Table A Userid, Group_type(A or B or C or D or E) ,Firstname, Lastname,email,addr1,addr2. Regards
Rajneesh
in such scenario, i think co-related subquery is the option as Ranjit said. Madhu
What does ‘top’ relate to in your case? That is, do you mean you want ‘top 50’ from each group in some implied order, or does order not matter?
If order does matter, then this becomes a bit trickier.
All other solutions posted do work, but they work according to SQL laws – there is no order.
(..or was that spoon?) <g> /Kenneth
Hi Kenneth, Thanks for your update. Order does not matter.
Regards
Rajneesh

Hi dineshasanka, Thanks for you answer. Regards
Rajneesh
]]>