Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 6B3949FB21C for ; Mon, 30 Oct 2006 23:32:50 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 02949-08 for ; Mon, 30 Oct 2006 23:32:47 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web31803.mail.mud.yahoo.com (web31803.mail.mud.yahoo.com [68.142.207.66]) by postgresql.org (Postfix) with SMTP id 5C6199F976E for ; Mon, 30 Oct 2006 23:32:47 -0400 (AST) Received: (qmail 14840 invoked by uid 60001); 31 Oct 2006 03:32:45 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=GruWMCVlMnh0wIPTjtEG0/fqAQvAbkZxBPOD8EQzFIG4TQuQNT8JDPKl4BvVBNBAmW7Np+I5qhLw58SPuu7MddLvPzC25lUOkoezAQ/2CPSLiRiYZEsYn9tOtLXUXWvL9aJ8GqqYjVwzT2OwzKc2abyh8+JOlz5vCeKgoTWP9HU= ; X-YMail-OSG: auvPT2IVM1lAXc7jBkg7JD4YNvlRNJDSQNX9KTBYQT3tGptzh5RdNATP29RcDJ9_E4OWqinpoTIBMa0zfKqg77Tz_7S_DD9tVajXZZzmMqG.5Lj2kJl9Dimu3953t79MkqkmTFcZIZjctaM- Received: from [24.54.196.198] by web31803.mail.mud.yahoo.com via HTTP; Mon, 30 Oct 2006 19:32:45 PST Date: Mon, 30 Oct 2006 19:32:45 -0800 (PST) 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: <20061031030120.40888.qmail@web50804.mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <610212.14371.qm@web31803.mail.mud.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.267 tagged_above=0 required=5 tests=AWL, BAYES_00, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, DNS_FROM_RFC_WHOIS X-Spam-Level: * X-Archive-Number: 200610/242 X-Sequence-Number: 26631 > select foo.ric, tm.times_time, count(tk.*), avg(tk.price), > sum(tk.price*tk.volume)/sum(tk.volume), sum(tk.volume) from (select distinct ric from ticks) 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 = foo.ric group by tm.times_time, > foo.ric order by tm.times_time; > > I get a error message like this: > > ERROR: invalid reference to FROM-clause entry for table "foo" > HINT: There is an entry for table "foo", but it cannot be referenced from this part of the > query. > > Can you help me with this? I will try, but to start with, to help us, when you have a difficult query to solve, you should simplify your query as much a possible. This way we can more quickly see what you are intending verses the problem you are having. 1 tip: (select distinct ric from ticks) I think that you will find that: (select ric from ticks group by ric) is much faster than using the distinct. The error in the query that I see is that you are using foo as a criteria in the ON syntax. This will not work. To illistrate: A,B join C ON (B.id = C.id) --ON syntax only works with joins AND (B.id2 < C.id) --The And is still part of the ON syntax --you can not reference A since it is not joined Where A.id = B.id --you can only specify a non-joined tables contrainst AND A.id2 < C.id2 ; --in the where clause I hope this helps. Regards, Richard Broersma JR.