Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 2CF519FA2BA for ; Thu, 26 Oct 2006 14:59:39 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 76762-10 for ; Thu, 26 Oct 2006 14:59:36 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web31806.mail.mud.yahoo.com (web31806.mail.mud.yahoo.com [68.142.207.69]) by postgresql.org (Postfix) with SMTP id CA6DD9FA2B9 for ; Thu, 26 Oct 2006 14:59:35 -0300 (ADT) Received: (qmail 78138 invoked by uid 60001); 26 Oct 2006 17:59:34 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=L65zGRczRzTvlyP3SE8qmNMifnYgUkcsUFUy+pgQrocGoheCEGclF+xCT6CjDv41z6jgjaWpkPgmu9E0SMVJhO3Jocx8Jml+6O3jEebVwFYcRNBMwhngLmgoD59IvJzNKLRARfVvJheJJ7naYXSXMvyDZvlINCk2wQhoC5p19ns= ; Message-ID: <20061026175934.78136.qmail@web31806.mail.mud.yahoo.com> X-YMail-OSG: SvnXOlUVM1mCx_E_0jR2OnUj0VlPhv24a7..7BHA_TdMeVy.G4uAhYYGcDO3TsdAuJWCqe.FYekfv.o06iHiR2CnssYTzuEwiU1VSwL9ARsEUK7VRqfEZFhC1o78r8fTR6gqddQJFoeuefI- Received: from [12.177.97.50] by web31806.mail.mud.yahoo.com via HTTP; Thu, 26 Oct 2006 10:59:34 PDT Date: Thu, 26 Oct 2006 10:59:34 -0700 (PDT) From: Richard Broersma Jr Subject: Re: Add calculated fields from one table to other table To: roopa perumalraja , pgsql-sql@postgresql.org In-Reply-To: <20061026010736.67473.qmail@web50815.mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=3.131 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, DNS_FROM_RFC_WHOIS X-Spam-Level: *** X-Archive-Number: 200610/210 X-Sequence-Number: 26599 > 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. Here is my guess how it can be done: insert into Timeseries ( tiker, time, avg_price, avg_volume ) select ... where select .... would be select tick, date_trunc('minute', time) as minute, avg(price) as avg_price, avg(volume) as avg_volume from ticker where time between 'yourstartdate' and 'yourenddate' group by tick, minute; Regards, Richard Broersma Jr.