Re: Add calculated fields from one table to other table

From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: roopa perumalraja <roopabenzer(at)yahoo(dot)com>, pgsql-sql(at)postgresql(dot)org
Cc: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
Subject: Re: Add calculated fields from one table to other table
Date: 2006-10-30 03:12:51
Message-ID: 534177.87743.qm@web31809.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> Thanks a lot for your help. The query does work, but now I have a problem. The query goes like
> this:
>
> select tk.ric, tm.timeseries_time, count(tk.*), avg(tk.price),
> sum(tk.price*tk.volume)/sum(tk.volume), sum(tk.volume)
> from ticks tk, timeseries tm where tk.tick_time >= tm.timeseries_time and
> tk.tick_time < (tm.timeseries_time + '1 minute' :: interval)::time group by tm.timeseries_time,
> tk.ric order by tk.ric, tm.timeseries_time
>
> The problem is, if there is no row for certain minute, then I want the count to be displayed
> as zero and other coulmns like avg to be null. In this query, it just omits those minutes which
> doesnt have any row for a particular minute.

You have to use an outer join. You will need a table or sequence that has every minute in a range
that you are interested in and outer join that to your actual table. This will give you a count
of zero.

i.e.

select S.minute, count(W.minute) as minutecnt

from Series_of_Minutes S left join Working_table W

on S.minute = W.minute
;

hope this helps.

REgards,

Richard Broersma jr.

ps. sorry that my query suggestion didn't work :0)

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message roopa perumalraja 2006-10-30 05:34:26 Re: Add calculated fields from one table to other table
Previous Message roopa perumalraja 2006-10-30 02:50:49 Re: Add calculated fields from one table to other table