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  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL using (min) and (max)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-18-11, 06:39
paulcurtis paulcurtis is offline
Registered User
 
Join Date: Jun 2011
Posts: 5
SQL using (min) and (max)

This is driving me nuts. I do hope someone can help this poor old duffer out.

Let's say we have the following two column database of city daily temperatures for a certain month. We can assume no two temperatures will ever be exacty the same. We don't know how many cities or rows there are:

London 20.1
Boston 15.5
Lincoln 18.1
Lincoln 13.0
London 21.1
Lincoln 12.9
Boston 17.6
London 20.3

If I do the following query:

select CITY, max(TEMP) MAXTEMP
from DATABASE
group by CITY

I will get:

CITY MAXTEMP
London 21.1
Boston 17.6
Lincoln 18.1

That's fine. Now let's say, out of those, I want the row with the MINIMUM temperature,.i.e.

Boston 17.6

I thought if I put the above query into a subquery, this would work:

select CITY, min(MAXTEMP)
from
(select CITY, max(TEMP) MAXTEMP
from DATABASE
group by CITY)

but it is syntactically incorrect. What am I doing wrong please?

Thanks.

Paul.
Reply With Quote
  #2 (permalink)  
Old 06-18-11, 07:24
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Code:
SELECT city,
       max_temp as min_temp
FROM (
    SELECT city, 
           max_temp,
           row_number() over (order by max_temp asc) as row_num
    FROM (
       SELECT city,
              max(temp) as max_temp
       FROM your_table
       GROUP BY city
    ) tm
) t
WHERE row_num = 1
An alternative would be:
Code:
WITH max_temperatures AS (
   SELECT city,
          max(temp) as max_temp
   FROM temperature
   GROUP BY city
) 
SELECT * 
FROM max_temperatures
WHERE max_temp = (SELECT min(max_temp)
                  FROM max_temperatures)

Last edited by shammat; 06-18-11 at 07:29.
Reply With Quote
  #3 (permalink)  
Old 06-18-11, 08:48
paulcurtis paulcurtis is offline
Registered User
 
Join Date: Jun 2011
Posts: 5
Thanks very much for that shammat, as you saw I had a reply in the DB2 forum too, so I have three solutions now
Reply With Quote
  #4 (permalink)  
Old 07-04-11, 06:16
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
please do not multipost the same question in several forums
it causes confusion
it it may not lead to the right answer
it may pee off contributors so they don't respond to your questions

...also FWIW a new question requires a new thread
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 08-17-11, 06:16
dbGuyo dbGuyo is offline
Registered User
 
Join Date: Jul 2011
Posts: 9
The other solution with just ANSI SQL would be:

Code:
select CITY2, temp2
from (select city city2, max(TEMP) temp2
      from t1
      group by CITY) a
where temp2 <= ALL (select max(TEMP) MAXTEMP
                    from t1
                    group by CITY);
But I don't claim that this is more efficient
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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