A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

How do I write an SQL statement to return the 2nd highest value?



 
 
Thread Tools Display Modes
  #1  
Old August 14th, 2005, 05:38 PM
Paul
external usenet poster
 
Posts: n/a
Default How do I write an SQL statement to return the 2nd highest value?

I need to write two seperate statements. One returns the max value from a
table and the other returns the 2nd highest value. Any help?
  #2  
Old August 14th, 2005, 05:47 PM
Ofer
external usenet poster
 
Posts: n/a
Default

You can write one sql that will return the two highest values, sort the sql
desc and return the two top values

Select Top 2 ValueField From TableName Order by ValueField Desc
========================================



"Paul" wrote:

I need to write two seperate statements. One returns the max value from a
table and the other returns the 2nd highest value. Any help?

  #3  
Old August 14th, 2005, 05:52 PM
Paul
external usenet poster
 
Posts: n/a
Default

Thanks, Ofer. But the thing is, I actually have to write 2 seperate
statements. One to return JUST the max value, and another to return only the
2nd highest value. Any ideas?

"Ofer" wrote:

You can write one sql that will return the two highest values, sort the sql
desc and return the two top values

Select Top 2 ValueField From TableName Order by ValueField Desc
========================================



"Paul" wrote:

I need to write two seperate statements. One returns the max value from a
table and the other returns the 2nd highest value. Any help?

  #4  
Old August 14th, 2005, 06:01 PM
Paul
external usenet poster
 
Posts: n/a
Default

Is there any way that I can use that TOP 2 query in conjunction with a
deletion of the the MAX value? That would leave me with only the 2nd highest
value, right?

"Ofer" wrote:

You can write one sql that will return the two highest values, sort the sql
desc and return the two top values

Select Top 2 ValueField From TableName Order by ValueField Desc
========================================



"Paul" wrote:

I need to write two seperate statements. One returns the max value from a
table and the other returns the 2nd highest value. Any help?

  #5  
Old August 14th, 2005, 06:11 PM
Ofer
external usenet poster
 
Posts: n/a
Default

Yes, when the top used as a filter.
try this:

DELETE TableName.*
FROM TableName
WHERE (((TableName.ValueField) In (select top 1 ValueField from TableName
order by ValueField desc)))



"Paul" wrote:

Is there any way that I can use that TOP 2 query in conjunction with a
deletion of the the MAX value? That would leave me with only the 2nd highest
value, right?

"Ofer" wrote:

You can write one sql that will return the two highest values, sort the sql
desc and return the two top values

Select Top 2 ValueField From TableName Order by ValueField Desc
========================================



"Paul" wrote:

I need to write two seperate statements. One returns the max value from a
table and the other returns the 2nd highest value. Any help?

  #6  
Old August 14th, 2005, 07:56 PM
John Spencer (MVP)
external usenet poster
 
Posts: n/a
Default

Why two separate statements?

First Query.
Select Max(Something) as Maximum
FROM YourTable

Second Query.
SELECT Max(Something) as 2ndMaximum
FROM YourTable
WHERE Something
(SELECT Max(Something)
FROM YourTable)



Paul wrote:

I need to write two seperate statements. One returns the max value from a
table and the other returns the 2nd highest value. Any help?

  #7  
Old April 1st, 2006, 10:49 PM posted to microsoft.public.access.queries
external usenet poster
 
Posts: n/a
Default How do I write an SQL statement to return the 2nd highest valu

I have a similar issue. I need to select the highest 5 test scores in a list
of test scores. However, some of the test scores may be the same for two or
more students. How do I get the list of the top 5 test scores like this:
95
91
91
86
84

Please let me know at
Thanks


"John Spencer (MVP)" wrote:

Why two separate statements?

First Query.
Select Max(Something) as Maximum
FROM YourTable

Second Query.
SELECT Max(Something) as 2ndMaximum
FROM YourTable
WHERE Something
(SELECT Max(Something)
FROM YourTable)



Paul wrote:

I need to write two seperate statements. One returns the max value from a
table and the other returns the 2nd highest value. Any help?


  #8  
Old April 2nd, 2006, 01:10 AM posted to microsoft.public.access.queries
external usenet poster
 
Posts: n/a
Default How do I write an SQL statement to return the 2nd highest valu

magicdds wrote:

I have a similar issue. I need to select the highest 5 test scores in a list
of test scores. However, some of the test scores may be the same for two or
more students. How do I get the list of the top 5 test scores like this:
95
91
91
86
84



SELECT TOP 5 score, student
FROM testscores
ORDER BY score DESC

You will get more than 5 only if there is a tie for fifth
place.

--
Marsh
MVP [MS Access]
  #9  
Old April 2nd, 2006, 01:16 AM posted to microsoft.public.access.queries
external usenet poster
 
Posts: n/a
Default How do I write an SQL statement to return the 2nd highest valu

try

SELECT TOP 5 TableName.ScoreField
FROM TableName
ORDER BY TableName.ScoreField DESC;

replace "TableName" and "ScoreField" with the correct names of the table and
the field.

note that "The TOP predicate does not choose between equal values. In the
preceding example [of TOP 25 grade point averages], if the twenty-fifth and
twenty-sixth highest grade point averages are the same, the query will
return 26 records."

hth


"magicdds" wrote in message
news
I have a similar issue. I need to select the highest 5 test scores in a
list
of test scores. However, some of the test scores may be the same for two

or
more students. How do I get the list of the top 5 test scores like this:
95
91
91
86
84

Please let me know at
Thanks


"John Spencer (MVP)" wrote:

Why two separate statements?

First Query.
Select Max(Something) as Maximum
FROM YourTable

Second Query.
SELECT Max(Something) as 2ndMaximum
FROM YourTable
WHERE Something
(SELECT Max(Something)
FROM YourTable)



Paul wrote:

I need to write two seperate statements. One returns the max value

from a
table and the other returns the 2nd highest value. Any help?




  #10  
Old August 25th, 2006, 02:06 PM posted to microsoft.public.access.queries
Nero
external usenet poster
 
Posts: 23
Default How do I write an SQL statement to return the 2nd highest valu

hello... i have a similar query, however i need to find the max value from
60%of the total entries...please help

"tina" wrote:

try

SELECT TOP 5 TableName.ScoreField
FROM TableName
ORDER BY TableName.ScoreField DESC;

replace "TableName" and "ScoreField" with the correct names of the table and
the field.

note that "The TOP predicate does not choose between equal values. In the
preceding example [of TOP 25 grade point averages], if the twenty-fifth and
twenty-sixth highest grade point averages are the same, the query will
return 26 records."

hth


"magicdds" wrote in message
news
I have a similar issue. I need to select the highest 5 test scores in a

list
of test scores. However, some of the test scores may be the same for two

or
more students. How do I get the list of the top 5 test scores like this:
95
91
91
86
84

Please let me know at
Thanks


"John Spencer (MVP)" wrote:

Why two separate statements?

First Query.
Select Max(Something) as Maximum
FROM YourTable

Second Query.
SELECT Max(Something) as 2ndMaximum
FROM YourTable
WHERE Something
(SELECT Max(Something)
FROM YourTable)



Paul wrote:

I need to write two seperate statements. One returns the max value

from a
table and the other returns the 2nd highest value. Any help?




 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Statement created from ItemsSelected from Several Listboxes.. Denis Bisson Using Forms 2 February 21st, 2005 02:48 AM
How do I write a statement to return a name when there are 3 poss. Bob K Setting Up & Running Reports 1 February 17th, 2005 02:28 AM
how can i write ansi sql quires equalent to ms access and sql ser. S.SRIKANTH,GUDIWADA,AP,INDIA Running & Setting Up Queries 1 December 16th, 2004 08:13 AM
Access and SQL AHopper General Discussion 26 August 3rd, 2004 07:01 PM
Function isn't available in expressions in query expression Reiner Harmgardt General Discussion 4 July 21st, 2004 09:30 AM


All times are GMT +1. The time now is 03:49 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.