Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 MAX and MIN value, need date.

Author  Topic 

Slason
Starting Member

3 Posts

Posted - 2011-11-03 : 10:54:00
We are building a website, which we register metrological observation. We have a "report" page, where you can report each week, month etc. We have listed the MAX, AVG and MIN value of the temperature of all the observations.

What SQL can I use to get the SQL to list when (date) the MAX and MIN temperature is observed?

My DB looks like this:

observations
- observationid (int)
- date (DATE)
- temperature (float)
(and some other rows irrelevant to this question)

So a summary of my question: How do I get sql to give me the MAX and MIN temperature, as well as their date?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-03 : 10:57:57
[code]
SELECT t.date,t.temperature
FROM observations t
INNER JOIN (SELECT MIN(temperature) AS val
FROM observations
UNION ALL
SELECT MAX(temperature) AS val
FROM observations)t1
On t1.Val = t.temperature
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Slason
Starting Member

3 Posts

Posted - 2011-11-03 : 11:02:03
Thx for quick reply! Really appreciated; you've given me lots of help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-03 : 11:04:57
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -