dbTalk Databases Forums  

Querying minima and maxima of a date column for several subsets ofthe same table

comp.databases.postgresql comp.databases.postgresql


Discuss Querying minima and maxima of a date column for several subsets ofthe same table in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Julia Jacobson
 
Posts: n/a

Default Querying minima and maxima of a date column for several subsets ofthe same table - 07-18-2010 , 02:37 AM






Hello everybody out there using PostgreSQL,

There is a table within my database which contains the dates with a
temperature of more than 30°C in 2009 for many different towns.
Here's a short excerpt of it:

town | date
-------------------
London | 2009-07-12
London | 2009-08-04
Berlin | 2009-06-30
Berlin | 2009-07-15
Berlin | 2009-07-29
Paris | 2009-06-25
Paris | 2009-07-05
Paris | 2009-07-06
Paris | 2009-08-14

I want to know the first and the last day with a temperature of more
than 30°C in 2009 for each town. What would be the SELECT statement in
SQL for that?

Thanks in advance,
Julia

Reply With Quote
  #2  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Querying minima and maxima of a date column for several subsetsof the same table - 07-18-2010 , 03:17 AM






Julia Jacobson wrote on 18.07.2010 09:37:
Quote:
Hello everybody out there using PostgreSQL,

There is a table within my database which contains the dates with a
temperature of more than 30°C in 2009 for many different towns.
Here's a short excerpt of it:

town | date
-------------------
London | 2009-07-12
London | 2009-08-04
Berlin | 2009-06-30
Berlin | 2009-07-15
Berlin | 2009-07-29
Paris | 2009-06-25
Paris | 2009-07-05
Paris | 2009-07-06
Paris | 2009-08-14

I want to know the first and the last day with a temperature of more
than 30°C in 2009 for each town. What would be the SELECT statement in
SQL for that?
SELECT town, min(date), max(date)
FROM the_table_with_no_name
GROUP BY town;

Reply With Quote
  #3  
Old   
Jasen Betts
 
Posts: n/a

Default Re: Querying minima and maxima of a date column for several subsetsof the same table - 07-18-2010 , 05:39 AM



On 2010-07-18, Julia Jacobson <julia.jacobson (AT) arcor (DOT) de> wrote:
Quote:
Hello everybody out there using PostgreSQL,

There is a table within my database which contains the dates with a
temperature of more than 30°C in 2009 for many different towns.
Here's a short excerpt of it:

town | date
-------------------
London | 2009-07-12
London | 2009-08-04
Berlin | 2009-06-30
Berlin | 2009-07-15
Berlin | 2009-07-29
Paris | 2009-06-25
Paris | 2009-07-05
Paris | 2009-07-06
Paris | 2009-08-14

I want to know the first and the last day with a temperature of more
than 30°C in 2009 for each town. What would be the SELECT statement in
SQL for that?
If that table does Sydney (or other southern hemisphere locations)
those results may be misleading.


--- news://freenews.netfront.net/ - complaints: news (AT) netfront (DOT) net ---

Reply With Quote
  #4  
Old   
Anselmo Canfora
 
Posts: n/a

Default Re: Querying minima and maxima of a date column for several subsetsof the same table - 07-18-2010 , 06:08 AM



Il 18/07/2010 9.37, Julia Jacobson ha scritto:
Quote:
Hello everybody out there using PostgreSQL,

There is a table within my database which contains the dates with a
temperature of more than 30°C in 2009 for many different towns.
Here's a short excerpt of it:

town | date
-------------------
London | 2009-07-12
London | 2009-08-04
Berlin | 2009-06-30
Berlin | 2009-07-15
Berlin | 2009-07-29
Paris | 2009-06-25
Paris | 2009-07-05
Paris | 2009-07-06
Paris | 2009-08-14

I want to know the first and the last day with a temperature of more
than 30°C in 2009 for each town. What would be the SELECT statement in
SQL for that?
Does your table contains _only_ the records with more than 30° _or_
_also_ the records of temperatures <30?

The general query, supposing that the table has dates in multiple years
and all temperatures:

select min(date) as oldest, max(date) as latest, town from
temperatures_table where temperature>30 and extract('year', date)='2009'
group by town;

Reply With Quote
  #5  
Old   
Anselmo Canfora
 
Posts: n/a

Default Re: Querying minima and maxima of a date column for several subsetsof the same table - 07-18-2010 , 06:10 AM



Il 18/07/2010 13.08, Anselmo Canfora ha scritto:
Quote:
Il 18/07/2010 9.37, Julia Jacobson ha scritto:
Hello everybody out there using PostgreSQL,

There is a table within my database which contains the dates with a
temperature of more than 30°C in 2009 for many different towns.
Here's a short excerpt of it:

town | date
-------------------
London | 2009-07-12
London | 2009-08-04
Berlin | 2009-06-30
Berlin | 2009-07-15
Berlin | 2009-07-29
Paris | 2009-06-25
Paris | 2009-07-05
Paris | 2009-07-06
Paris | 2009-08-14

I want to know the first and the last day with a temperature of more
than 30°C in 2009 for each town. What would be the SELECT statement in
SQL for that?

Does your table contains _only_ the records with more than 30° _or_
_also_ the records of temperatures <30?

The general query, supposing that the table has dates in multiple years
and all temperatures:

select min(date) as oldest, max(date) as latest, town from
temperatures_table where temperature>30 and extract('year', date)='2009'
group by town;

errata corrige:

select min(date) as oldest, max(date) as latest, town from
temperatures_table where temperature>30 and extract('year', date)=2009
group by town;

Reply With Quote
  #6  
Old   
Julia Jacobson
 
Posts: n/a

Default Re: Querying minima and maxima of a date column for several subsetsof the same table - 07-18-2010 , 07:55 AM



Thanks a lot for your answers.
I'm sorry, but I didn't knew anything about the GROUP BY statement
before, since I'm pretty new to databases.
Using the GROUP BY statement in combination with a normal SELECT query
works perfectly.

Reply With Quote
  #7  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Querying minima and maxima of a date column for several subsetsof the same table - 07-18-2010 , 03:25 PM



On Sun, 18 Jul 2010 09:37:33 +0200, Julia Jacobson wrote:

Quote:
Hello everybody out there using PostgreSQL,

There is a table within my database which contains the dates with a
temperature of more than 30°C in 2009 for many different towns. Here's a
short excerpt of it:

town | date
-------------------
London | 2009-07-12
London | 2009-08-04
Berlin | 2009-06-30
Berlin | 2009-07-15
Berlin | 2009-07-29
Paris | 2009-06-25
Paris | 2009-07-05
Paris | 2009-07-06
Paris | 2009-08-14

I want to know the first and the last day with a temperature of more
than 30°C in 2009 for each town. What would be the SELECT statement in
SQL for that?

Thanks in advance,
Julia
scott=# copy temp from '/tmp/temp.txt' with delimiter as '|';
COPY 9
Time: 10.307 ms
scott=# select * from temp;
town | temp
---------+------------
London | 2009-07-12
London | 2009-08-04
Berlin | 2009-06-30
Berlin | 2009-07-15
Berlin | 2009-07-29
Paris | 2009-06-25
Paris | 2009-07-05
Paris | 2009-07-06
Paris | 2009-08-14
(9 rows)

Time: 0.374 ms
scott=# select town,min(temp),max(temp)
scott-# from temp
scott-# group by town;
town | min | max
---------+------------+------------
Berlin | 2009-06-30 | 2009-07-29
London | 2009-07-12 | 2009-08-04
Paris | 2009-06-25 | 2009-08-14
(3 rows)

Time: 47.435 ms
scott=#


--
http://mgogala.byethost5.com

Reply With Quote
Reply




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 On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.