user2637506 user2637506 - 1 month ago 12
SQL Question

How do we select top 20% and bottom 80% records in sql server

I am trying to select top 20% ordered results and also remaining (bottom 80%) in a table.

when i do

select top (20)percent from table 1


and later

select top (80)percent from table 1 order by desc


as two different queries i get the result. But my problem here is , if the record has same values for the ordered column, then i might pick the same record again, as the selection is not content.

Can you suggest with any good way so that i can select top 20% distinct and remaining 80% records in a table.

Ben Ben
Answer

To get your top 20 percent is:

select top (20) percent from table1 order by <something>

If you want the remaining 80% it's everything that's not in the top 20%; so:

select * from table1
except
select top(20) percent from table1 order by <something>
Comments