Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select

Author  Topic 

Masum7
Starting Member

33 Posts

Posted - 2010-06-30 : 04:52:11
I have a table like this:

id--status
----------------
1---1
2---1
3---0
4---0
5---1
6---0
-----------

Now what is the best way to select top 50 rows where first 25 will be status=1 and second 25 will be status=0
So, the simplest and fastest query will select like this:
id--status
----------------
1---1
2---1
5---1
3---0
4---0
6---0
-----------


Masum

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-30 : 04:59:53
select id,status from
(
select ntile(2) over (order by status desc) as sno,* from table
) as t
order by sno


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -