PC Review


Reply
Thread Tools Rate Thread

How do I get the second lowest by using SQL/Query

 
 
nankerp
Guest
Posts: n/a
 
      29th Apr 2009
To get the lowest value is by using SELECT MIN(Sale) AS Low From
tblSale. But how do I get the second lowest. Are there any function to
choose nr 2 or 3 from bottom?

Helge
 
Reply With Quote
 
 
 
 
Paul Shapiro
Guest
Posts: n/a
 
      30th Apr 2009
Use a derived query. Untested Access sql:
Select min(Sale) as NthLowest
From tblSale
Where Sale > Any (Select Top n Sale From tblSale Order by Sale)

Replace Top n with whatever number you want. The subquery gets the n rows
with the lowest values. If you had 15 rows with the same lowest value, this
query return that same value for the 2nd-lowest row. If you want to look at
values, not rows, change the Where clause to:
Where Sale > Any (Select Top n Q2.Sale From (Select Distinct tblSale.Sale
From tblSale) As Q2 Order by Q2.Sale)

This version takes the distinct sales values in the innermost query. The
next subquery gets the nth lowest values. The outer query gets the smallest
value larger than that nth-lowest value.

"nankerp" <(E-Mail Removed)> wrote in message
news:c98893f0-4731-4706-b671-(E-Mail Removed)...
> To get the lowest value is by using SELECT MIN(Sale) AS Low From
> tblSale. But how do I get the second lowest. Are there any function to
> choose nr 2 or 3 from bottom?
>
> Helge


 
Reply With Quote
 
John Spencer MVP
Guest
Posts: n/a
 
      30th Apr 2009
One way

SELECT Min(Sale) as SecondLow
FROM tblSale
WHERE Sale <>
(SELECT Min(Sale) as FirstLow from tblSale)

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

nankerp wrote:
> To get the lowest value is by using SELECT MIN(Sale) AS Low From
> tblSale. But how do I get the second lowest. Are there any function to
> choose nr 2 or 3 from bottom?
>
> Helge

 
Reply With Quote
 
Helge
Guest
Posts: n/a
 
      1st May 2009
On 30 Apr, 18:23, John Spencer MVP <spen...@chpdm.edu> wrote:
> One way
>
> SELECT Min(Sale) as SecondLow
> FROM tblSale
> WHERE Sale <>
> * *(SELECT Min(Sale) as FirstLow from tblSale)
>
> John Spencer
> Access MVP 2002-2005, 2007-2009
> The Hilltop Institute
> University of Maryland Baltimore County
>
>
>
> nankerp wrote:
> > To get the lowest value is by using SELECT MIN(Sale) AS Low From
> > tblSale. But how do I get the second lowest. Are there any function to
> > choose nr 2 or 3 from bottom?

>
> > Helge– Skjul sitert tekst –

>
> – Vis sitert tekst –


Thank you very much. This sql work very well.

Helge
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I get the second lowest by using SQL/Query Helge Microsoft Access 0 29th Apr 2009 08:36 PM
How do I get the second lowest by using SQL/Query nankerp Microsoft Access 0 29th Apr 2009 08:02 PM
determine lowest number in several columns and replace lowest numb jerry Microsoft Excel Worksheet Functions 1 18th Jun 2008 03:19 AM
How do I get a crosstab query to sum the top n lowest numbers? =?Utf-8?B?UGhpbA==?= Microsoft Access Queries 1 23rd May 2006 04:14 PM
Nested Query only returning lowest number Nine Doors Microsoft Access Queries 5 22nd Jul 2004 05:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:22 AM.