Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 302CA63287E for ; Fri, 24 Jul 2009 19:42:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 41717-06 for ; Fri, 24 Jul 2009 22:42:16 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp.insa-lyon.fr (criges14.insa-lyon.fr [134.214.76.242]) by mail.postgresql.org (Postfix) with ESMTP id 6756C633388 for ; Fri, 24 Jul 2009 19:42:16 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by smtp.insa-lyon.fr (Postfix) with ESMTP id AA647F1393; Sat, 25 Jul 2009 00:42:13 +0200 (CEST) X-Virus-Scanned: SMTP at INSA-LYON Received: from smtp.insa-lyon.fr ([127.0.0.1]) by localhost (criges14.insa-lyon.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id DqB9Fq+KkKzh; Sat, 25 Jul 2009 00:42:13 +0200 (CEST) Received: from [127.0.0.1] (liris.univ-lyon1.fr [134.214.142.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: nhandrianar) by smtp.insa-lyon.fr (Postfix) with ESMTPSA id F2935F138B; Sat, 25 Jul 2009 00:42:12 +0200 (CEST) Message-ID: <4A6A3886.3080104@free.fr> Date: Sat, 25 Jul 2009 00:41:10 +0200 From: nha Organization: Lyon, Rhone, France User-Agent: Thunderbird 2.0.0.6 (Macintosh/20070728) MIME-Version: 1.0 To: Bartjoosen CC: PgSQL-sql Subject: Re: using count in other column References: <24622738.post@talk.nabble.com> In-Reply-To: <24622738.post@talk.nabble.com> 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=0 tagged_above=-10 required=5 tests=none X-Spam-Level: X-Archive-Number: 200907/125 X-Sequence-Number: 32866 Hello, Le 23/07/09 11:59, bartjoosen a écrit : > Hi, > > I made up a query to make a count for each item for each month/year: > SELECT"Artnr_ID", to_char("Date_plan","YYYY") AS "Jaar", > to_char("Date_plan","MM") AS "Maand", Count("tblArtnrs"."Artikelnr") AS > "Monthly_count", "val1","val2","val3" > FROM (("tblAnalyses" INNER JOIN "tblStudies" ON "tblAnalyses"."Studie_ID" = > "tblStudies"."Studie_ID") INNER JOIN "tblFichenr" ON > "tblStudies"."ID_fichenr" = "tblFichenr"."ID") INNER JOIN "tblArtnrs" ON > "tblFichenr"."ID_Art_nrs" = "tblArtnrs"."Artnr_ID" > GROUP BY "tblArtnrs"."Artnr_ID", to_char("Date_plan","YYYY") , > to_char("Date_plan","MM"), "val1","val2","val3"; > > Now I want to use the "Monthly_count" value for further calculations with > other columns. > I tried to use > "Monthly_count"+ "val1" + "Monthly_count" * "val2" + "Monthly_count" *"val3" > But "Monthly_count" is not recognised in my calculations. > > How can this be solved? > > Thanks > > Bart > The error message you meet is missing in your report although it could surely help in accurate analysis. However I guess it is about using alias (like "Monthly_count") for defining project columns (like the one you tried and failed). Alias columns are used to rename displayed columns; they cannot be used as terms of other projected columns but they can be used within GROUP BY clauses. For example, the given GROUP BY clause may be rewritten (simplier) as: GROUP BY "Artnr_ID", "Artnr_ID", "Jaar", "Maand", "val1", "val2", "val3" and even as: GROUP BY "Artnr_ID", "Date_plan", "val1", "val2", "val3" as "Jaar" and "Maand" refer to column "Date_plan" which is not passed to aggregating functions but is effectively aggregated. Hoping this is a track for you. Regards. -- nha / Lyon / France.