Home All Groups Group Topic Archive Search About

Second hishest record in the table

Author
9 Jul 2005 6:41 AM
Sekhon
how we can get the second highest record in the table . for example I have
table "StudScores" with fields (studID int,Studname,StuDScores)

I wants the query to find out Record in the "StudScores"  table of  the
Student which  has second highest score

Author
9 Jul 2005 7:16 AM
Ben Amada
"Sekhon" wrote:

> how we can get the second highest record in the table . for example I have
> table "StudScores" with fields (studID int,Studname,StuDScores)
>
> I wants the query to find out Record in the "StudScores"  table of  the
> Student which  has second highest score

Untested, but something like this should work:

SELECT TOP 1 s.StudID, s.StudName, s.StudScores
FROM StudScores AS s
WHERE s.StudScores <
  ( SELECT MAX(s1.StudScores)
    FROM StudScores AS s1 )
ORDER BY s.StudScores

Ben
Author
9 Jul 2005 1:10 PM
Mercury
Think set theory next time and the answers just given will pop out.

Show quote
"Ben Amada" <b**@REpoMOweVErpick.com> wrote in message
news:OdF19YFhFHA.1460@tk2msftngp13.phx.gbl...
> "Sekhon" wrote:
>
>> how we can get the second highest record in the table . for example I
>> have
>> table "StudScores" with fields (studID int,Studname,StuDScores)
>>
>> I wants the query to find out Record in the "StudScores"  table of  the
>> Student which  has second highest score
>
> Untested, but something like this should work:
>
> SELECT TOP 1 s.StudID, s.StudName, s.StudScores
> FROM StudScores AS s
> WHERE s.StudScores <
>  ( SELECT MAX(s1.StudScores)
>    FROM StudScores AS s1 )
> ORDER BY s.StudScores
>
> Ben
>
Author
9 Jul 2005 9:11 AM
oj
you could also try this:

select top 1 * with ties
from (select top 2 * with ties
from studscores
order by studscores desc)
order by studscores asc

--
-oj


Show quote
"Sekhon" <Sek***@discussions.microsoft.com> wrote in message
news:C5E08E4F-D10D-4C43-BDFB-A2D75B13B31E@microsoft.com...
> how we can get the second highest record in the table . for example I have
> table "StudScores" with fields (studID int,Studname,StuDScores)
>
> I wants the query to find out Record in the "StudScores"  table of  the
> Student which  has second highest score
Author
9 Jul 2005 12:43 PM
--CELKO--
>> how we can get the second highest record [sic] in the table. <<

Rows are not records, and columns are not fields.

>>  I have table "StudScores" with fields (studID nt,Studname,StuDScores) <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications.  It is very hard to debug code when you do not let us
see it.

>> I want a the query to find out Record [sic] in the "StudScores"  table of  the
Student which  has second highest score  <<

There are many ways to do this.  Here is a pure ANSI version:

SELECT stud_id, stud_name, stud_score
FROM StudScores AS S1
WHERE  S1.stud_score
          = ( SELECT MAX(S2.stud_score)
                 FROM StudScores AS S2
               WHERE stud_score
                          < (SELECT MAX(S3.stud_score)
                                    FROM StudScores AS S3));

ORDER BY s.StudScores
Author
9 Jul 2005 3:11 PM
Anith Sen
>> There are many ways to do this.  Here is a pure ANSI version:

This approach would require higher levels of nesting of subqueries if the OP
wanted 3rd highest, 4th highest etc. For an ANSI version, I would recommend
using the logic behind the usual ranking method, which is much simpler and
efficient.

--
Anith
Author
9 Jul 2005 7:26 PM
--CELKO--
This one is due to Marc Sokol and he did not go beyond three levels.
DB2 keeps the top two values in the Stats to make a rough histogram of
the distribution using five values (top two, average and lower two).
It is one of these weird implementation tricks.

AddThis Social Bookmark Button