pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Aaron Bono <postgresql@aranya.com>
To: roopa perumalraja <roopabenzer@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Add calculated fields from one table to other table
Date: Wed, 25 Oct 2006 20:59:51 -0500
Message-ID: <bf05e51c0610251859j38c53873la0bfdc404d3b82d2@mail.gmail.com> (raw)
In-Reply-To: <20061026010736.67473.qmail@web50815.mail.yahoo.com>
References: <20061026010736.67473.qmail@web50815.mail.yahoo.com>

On 10/25/06, roopa perumalraja <roopabenzer@yahoo.com> wrote:
>
> Hi
>
> I have two tables. Tick table has fields like ticker, time, price & volume
> and Timeseries table has fields like ticker, time, avg_price, avg_volume.
>
> The time field in Timeseries table is different from time in tick table,
> its the timeseries for every minute. Now I want to calculate the average
> price & volume from tick table for each ticker and for every minute and add
> those fields to timeseries table. Can anyone please help me out with the sql
> query.
>
> Note: The ticker in the tick table also has duplicate values, so i am not
> able to create relation between two tables.
>

Will this help:

select
    ticker,
    date_trunc('minute', time),
    ave(price),
    ave(volume)
from tick
group by
    ticker,
    date_trunc('minute', time)

You say you want to "add" these values to the Timeseries table?  You mean
insert them?  If so do this:

insert into timeseries (
ticker, time, avg_price, avg_volume
)
select
    ticker,
    date_trunc('minute', time),
    ave(price),
    ave(volume)
from tick
group by
    ticker,
    date_trunc('minute', time)

Of course if you do this repeatedly, you will start gathering duplicates in
the timeseries so you may want to do one insert and one update:

insert into timeseries (
ticker, time, avg_price, avg_volume
)
select
    tick.ticker,
    date_trunc('minute', tick.time),
    ave(tick.price),
    ave(tick.volume)
from tick
left outer join timeseries on (
    -- Not sure your join since you said time is not the same between ticke
and timeseries
    date_trunc('minute', tick.time) = timeseries.tick
    and tick.ticker = timeseries.ticker
)
group by
    ticker,
    date_trunc('minute', time)
having timeseries.ticker is null

... I will leave the update as an exercise ;)

==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
==================================================================

view thread (18+ messages)  latest in thread

Message-ID: <bf05e51c0610251859j38c53873la0bfdc404d3b82d2@mail.gmail.com>
Permalink:  ../bf05e51c0610251859j38c53873la0bfdc404d3b82d2@mail.gmail.com/
Also on:    postgresql.org/message-id/bf05e51c0610251859j38c53873la0bfdc404d3b82d2@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: postgresql@aranya.com, roopabenzer@yahoo.com
  Subject: Re: Add calculated fields from one table to other table
  In-Reply-To: <bf05e51c0610251859j38c53873la0bfdc404d3b82d2@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox