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 > Database Server Software > DB2 > SQL using (min) and (max)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-18-11, 06:36
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, 06:38
paulcurtis paulcurtis is offline
Registered User
 
Join Date: Jun 2011
Posts: 5
Sorry, I meant to post this on the SQ board, and will cross-post, but any replies here welcome too!
Reply With Quote
  #3 (permalink)  
Old 06-18-11, 08:00
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Here is an example on DB2.

Code:
------------------------------ Commands Entered ------------------------------
WITH
 city_daily_temperature
(city , temperature) AS (
VALUES
  ('London'  , 20.1)
, ('Boston'  , 15.5)
, ('Lincoln' , 18.1)
, ('Lincoln' , 13.0)
, ('London'  , 21.1)
, ('Lincoln' , 12.9)
, ('Boston'  , 17.6)
, ('London'  , 20.3)
)
SELECT city
     , max_temp
 FROM  (SELECT city
             , MAX(temperature) AS max_temp
             , MIN( MAX(temperature) )
                  OVER()        AS min_max_temp
         FROM  city_daily_temperature
         GROUP BY
               city
       )
 WHERE max_temp = min_max_temp
;
------------------------------------------------------------------------------

CITY    MAX_TEMP
------- --------
Boston      17.6

  1 record(s) selected.
Reply With Quote
  #4 (permalink)  
Old 06-18-11, 08:08
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
That's a nice one

I didn't think of combining the min/max into a single query
Reply With Quote
  #5 (permalink)  
Old 06-18-11, 08:44
paulcurtis paulcurtis is offline
Registered User
 
Join Date: Jun 2011
Posts: 5
Thanks so much for that fast, that works brilliantly.
Reply With Quote
  #6 (permalink)  
Old 06-18-11, 17:18
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
you also might try this:


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

ORDER BY MAXTEMP FETCH FIRST ROW ONLY
Reply With Quote
  #7 (permalink)  
Old 06-18-11, 18:57
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
If you used "FETCH FIRST 1 ROW ONLY",
subquery would be unnecessary.

Tested on DB2 Express-C 9.7 FP4 for Windows.
Code:
------------------------------ Commands Entered ------------------------------
WITH
 city_daily_temperature
(city , temperature) AS (
VALUES
  ('London'  , 20.1)
, ('Boston'  , 15.5)
, ('Lincoln' , 18.1)
, ('Lincoln' , 13.0)
, ('London'  , 21.1)
, ('Lincoln' , 12.9)
, ('Boston'  , 17.6)
, ('London'  , 20.3)
)
SELECT city
     , MAX(temperature) AS max_temp
 FROM  city_daily_temperature
 GROUP BY
       city
 ORDER BY
       max_temp
 FETCH FIRST 1 ROW ONLY
;
------------------------------------------------------------------------------

CITY    MAX_TEMP
------- --------
Boston      17.6

  1 record(s) selected.

Last edited by tonkuma; 06-18-11 at 18:59. Reason: Change ORDER BY MAX(temperature) to ORDER BY max_temp
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