| Author |
Topic |
|
arorarahul.0688
Posting Yak Master
125 Posts |
Posted - 2008-02-04 : 07:24:34
|
| hi all,can plz any one let me to understand the fllow of following queryUse of corelated query To find the nth highest salary:-Select Salary from Table1 T1 where N =(Select Count(Distinct(Salary)) From Table1 T2 whereT2.Salary >= T1.Salary)Where N is the desired no for which highest salary to be fetched.where Table1 is Name| Salaryasdf| 12345qwe | 2351sdf | 5684gtr | 3210mnnb| 321I am clear of the funda that first outer query will be executed then Inner query ..but how the comparison is going that is not clear to me plz if any one can help me to understand that.....Rahul Arora 07 BatchNCCE Israna, ######################IMPOSSIBLE = I+M+POSSIBLE |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-04 : 07:27:13
|
| the inner query will give count of unique salaries which is greater than or equal to the salary passed by outer query.when this count is equated to N, we get salary for which there are N unique salaries existing which is equal to or greater than current one. This is in other words same as Nth highest salary. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-04 : 07:33:22
|
Are you using SQL Server 2000 or SQL Server 2005? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
arorarahul.0688
Posting Yak Master
125 Posts |
Posted - 2008-02-04 : 07:46:00
|
I am using sql server 2000. can this affect the result?quote: Originally posted by Peso Are you using SQL Server 2000 or SQL Server 2005? E 12°55'05.25"N 56°04'39.16"
Rahul Arora 07 BatchNCCE Israna, ######################IMPOSSIBLE = I+M+POSSIBLE |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-04 : 07:51:31
|
quote: Originally posted by arorarahul.0688 hi all,can plz any one let me to understand the fllow of following queryUse of corelated query To find the nth highest salary:-Select Salary from Table1 T1 where N =(Select Count(Distinct(Salary)) From Table1 T2 whereT2.Salary >= T1.Salary)Where N is the desired no for which highest salary to be fetched.where Table1 is Name| Salaryasdf| 12345qwe | 2351sdf | 5684gtr | 3210mnnb| 321I am clear of the funda that first outer query will be executed then Inner query ..but how the comparison is going that is not clear to me plz if any one can help me to understand that.....Rahul Arora 07 BatchNCCE Israna, ######################IMPOSSIBLE = I+M+POSSIBLE
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/find-nth-maximum-value.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
arorarahul.0688
Posting Yak Master
125 Posts |
Posted - 2008-02-04 : 08:01:24
|
thanks for rplybt i have doubt lets take another examplethis is a tablea | rollwr 2aa 3aa 4bee 5as 123as 12as 23as 232select * from testing t1 where 4 = ( select Count(Distinct(roll)) from testing t2 where t2.roll >= t1.roll)resultsas 12according to corelated query mechanism inner query runs for each row given by outer query so how 12 can be 4th highest as in pass 12 will be compared as 2>=2 results 1 2>=3 results 0 and so onin pass23 will be compared as 3>=2 results 1 3>=3 results 1 3>=4 results 0 and so onin pass35 will be compared as 4>=2 results 1 4>=3 results 1 4>=4 results 1 4>=5 results 0 and so onin pass45 will be compared as 5>=2 results 1 5>=3 results 1 5>=4 results 1 5>=5 results 1here no counts those are true for the give condition becomes 4 so 5 should be returned as result but as result i am getting 12 can u please tell me why ?quote: Originally posted by visakh16 the inner query will give count of unique salaries which is greater than or equal to the salary passed by outer query.when this count is equated to N, we get salary for which there are N unique salaries existing which is equal to or greater than current one. This is in other words same as Nth highest salary.
Rahul Arora 07 BatchNCCE Israna, ######################IMPOSSIBLE = I+M+POSSIBLE |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-04 : 08:24:19
|
[code]For 'wr' and 2, get number of records that is 2 or more for Roll.Result is 8 which means this is the 8th highest value.For 'aa' and 3, get number of records that is 3 or more for Roll.Result is 7 which means this is the 7th highest value.For 'aa' and 4, get number of records that is 4 or more for Roll.Result is 6 which means this is the 6th highest value.For 'bee' and 5, get number of records that is 5 or more for Roll.Result is 5 which means this is the 5th highest value.For 'as' and 123, get number of records that is 123 or more for Roll.Result is 2 which means this is the 2nd highest value.For 'as' and 12, get number of records that is 12 or more for Roll.Result is 4 which means this is the 4th highest value.For 'as' and 23, get number of records that is 23 or more for Roll.Result is 3 which means this is the 3rd highest value.For 'as' and 232, get number of records that is 232 or more for Roll.Result is 1 which means this is the 1st highest value.[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
arorarahul.0688
Posting Yak Master
125 Posts |
Posted - 2008-02-04 : 08:33:05
|
thanks Peso thanks a lotnow i got it. thanks again.quote: Originally posted by Peso
For 'wr' and 2, get number of records that is 2 or more for Roll.Result is 8 which means this is the 8th highest value.For 'aa' and 3, get number of records that is 3 or more for Roll.Result is 7 which means this is the 7th highest value.For 'aa' and 4, get number of records that is 4 or more for Roll.Result is 6 which means this is the 6th highest value.For 'bee' and 5, get number of records that is 5 or more for Roll.Result is 5 which means this is the 5th highest value.For 'as' and 123, get number of records that is 123 or more for Roll.Result is 2 which means this is the 2nd highest value.For 'as' and 12, get number of records that is 12 or more for Roll.Result is 4 which means this is the 4th highest value.For 'as' and 23, get number of records that is 23 or more for Roll.Result is 3 which means this is the 3rd highest value.For 'as' and 232, get number of records that is 232 or more for Roll.Result is 1 which means this is the 1st highest value. E 12°55'05.25"N 56°04'39.16"
Rahul Arora 07 BatchNCCE Israna, ######################IMPOSSIBLE = I+M+POSSIBLE |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-04 : 09:24:19
|
| Also refer the 4th method described in the link I posted which is effecientMadhivananFailing to plan is Planning to fail |
 |
|
|
|