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 2000 Forums
 Transact-SQL (2000)
 Explanation Required to a Query

Author  Topic 

Jaswinder
Starting Member

8 Posts

Posted - 2007-04-27 : 03:24:18
----Can anyone please explain how this querie works.-------------

Objective : To find second highest value (here id) from a table APState .

I got this code from a forum.
-----------------------------------------------------------------
select id from APState a where 4=(select count(distinct(id)) from APState b where a.id<=b.id);
----------------------------------------------------------------

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-04-27 : 05:09:52
There are so many ways to get 1, 2 3.... max/min from a table, the above also one of a methods.
The above query will give the 4th max of the column

It takes the count of values, how many values are greater in b for each value of a, if that count satisfies will give the nth max/min. distinct to avoid same value present in the table
Go to Top of Page
   

- Advertisement -