Skip site navigation (1) Skip section navigation (2)

Getting current and average on a single row

From: Mark Morgan Lloyd <markMLl(dot)pgsql-general(at)telemetry(dot)co(dot)uk>
To: pgsql-general(at)PostgreSQL(dot)org
Subject: Getting current and average on a single row
Date: 2010-11-24 09:08:27
Message-ID: icikmc$c8t$1@pye-srv-01.telemetry.co.uk (view raw or flat)
Thread:
Lists: pgsql-general
What is best practice when extracting both current and average from a 
table? Demonstration table here contains data from a cheap weather station.

I can obviously get the current reading like this:

select temp_out, dewpoint
from weather
where datetime between (now() - '10 minutes'::interval) and now()
order by datetime desc
limit 1;

and I can get averages like this:

select avg(temp_out) as avg_temp_out, avg(dewpoint) as avg_dewpoint
from weather
where datetime between (now() - '45 minutes'::interval) and now();

In both cases there are a dozen or so columns in total. How are these 
best merged to yield a single row? Some form of join, or window functions?

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

Responses

pgsql-general by date

Next:From: Grzegorz JaƛkiewiczDate: 2010-11-24 09:37:02
Subject: Re: alter table add column - specify where the column will go?
Previous:From: Dmitriy IgrishinDate: 2010-11-24 08:22:20
Subject: Re: alter table add column - specify where the column will go?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group