pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: roopa perumalraja <roopabenzer@yahoo.com>
To: pgsql-sql@postgresql.org, Richard Broersma Jr <rabroersma@yahoo.com>
Subject: Re: Add calculated fields from one table to other table
Date: Tue, 31 Oct 2006 19:54:40 -0800 (PST)
Message-ID: <20061101035440.76569.qmail@web50809.mail.yahoo.com> (raw)
In-Reply-To: <404822.35369.qm@web31809.mail.mud.yahoo.com>

Hi Richard,
   
  Thanks a lot. I still am not able to get the result for all the rics in the ticks table but I am able to get the result for a particular ric.
   
  Can you help me with getting the result for all the rics in the ticks table
   
  Thanks
  Roopa

Richard Broersma Jr <rabroersma@yahoo.com> wrote:
  > Thanks for your help. That does make sense, but I am not able to get the result what I wanted
> exactly. Let me explain you.
> 
> I have ticks table in which I have columns like ric, tick_time, price & volume. The times
> table has just one column with times_time which has time data for each minute ie.) 
> 
> Ticks
> ric | tick_time | price | volume
> A | 12:00:01 | 23.00 | 12
> A | 12:00:02 | 26.00 | 7
> B | 12: 00:02 | 8.00 | 2
> B | 12:01:01 | 45.00 | 6
> 
> Times
> times_time
> 12:00
> 12:01
> 12:02
> 
> Now I want the timeseries for each minute for all ric in the tick table. So my query goes like
> this for a particular ric say for example ric 'A'
> 
> select foo.ric, tm.times_time, count(tk.*), avg(tk.price), sum
> (tk.price*tk.volume)/sum(tk.volume), sum(tk.volume) from (select ric from ticks where ric = 'A'
> group by ric) as foo, times tm left join ticks tk on tk.tick_time >= tm.times_time and
> tk.tick_time < (tm.times_time + '1 minute' :: interval)::time and tk.ric = 'A' group by
> tm.times_time, foo.ric order by tm.times_time; 
> 
> I get the result as I expect, but i am not able to derive a query for all rics in the tick
> table.
> 
> I really appreciate your help.

Sorry I prematurely sent my first email

Select foo.ric,
date_trunc("minute", tm.times_time) as time_tick,
count(tk.*) tickperminute,
avg(tk.price),
... --your other aggregate functions
from (
select ric 
from ticks 
where ric = 'A' 
group by ric
) as foo
join ticks tk 
on (tk.ric = foo.ric) 
right join times tm
on (tk.tick_time >= tm.times_time) 
and (tk.tick_time < (tm.times_time + '1 minute' :: interval)::time 
and (tk.ric = 'A') -- this shouldn't be neccessary
-- if you restructor your join
-- since foo limits all ric to 'A'
-- but since it is on the wrong side
-- of an outer join it can't.
group by foo.ric, 
time_tick 

order by time_tick;

Regards,

Richard Broersma Jr.


 
---------------------------------
Low, Low, Low Rates! Check out Yahoo! Messenger's cheap  PC-to-Phone call rates.

view thread (18+ messages)  latest in thread

Message-ID: <20061101035440.76569.qmail@web50809.mail.yahoo.com>
Permalink:  ../20061101035440.76569.qmail@web50809.mail.yahoo.com/
Also on:    postgresql.org/message-id/20061101035440.76569.qmail@web50809.mail.yahoo.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: roopabenzer@yahoo.com, rabroersma@yahoo.com
  Subject: Re: Add calculated fields from one table to other table
  In-Reply-To: <20061101035440.76569.qmail@web50809.mail.yahoo.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