SQL Server Performance Forum – Threads Archive
Top X recordsI 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
please post your table structure
How about this Select Top 50 * from Table where Fld =’A’
Select Top 50 * from Table where Fld =’B’
Select Top 50 * from Table where Fld =’C’
Select Top 50 * from Table where Fld =’D’ —————————————-
Contributing Editor, Writer & Forums Moderator
http://www.SQL-Server-Performance.Com Visit my Blog at
consider this approach: select * from maintable t
where col1 in(
select top 50 col1 from maintable t1
Table A Userid, Group_type(A or B or C or D or E) ,Firstname, Lastname,email,addr1,addr2. Regards
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.
Hi dineshasanka, Thanks for you answer. Regards