|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Second hishest record in the tablehow 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 "Sekhon" wrote: Untested, but something like this should work:> 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 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 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 > 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 -- Show quote-oj "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 >> 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 >> 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 |
|||||||||||||||||||||||