Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id A4C396329C3 for ; Fri, 4 Jun 2010 02:54:44 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 17987-04 for ; Fri, 4 Jun 2010 05:54:37 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.171]) by mail.postgresql.org (Postfix) with ESMTP id 70CD36323AF for ; Fri, 4 Jun 2010 02:54:37 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrbap2) with ESMTP (Nemesis) id 0MOlGA-1OQRje0n5S-005o6g; Fri, 04 Jun 2010 07:54:36 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1OKPrX-00068H-00 for pgsql-sql@postgresql.org; Fri, 04 Jun 2010 07:54:35 +0200 Date: Fri, 4 Jun 2010 07:54:34 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: sum an alias Message-ID: <20100604055434.GA19411@a-kretschmer.de> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX1/oE+MkJqDAMek+DdrYu+Ig9WsPoTQS5EtoDd5 whKv5dhZadzr9H1MRFPIyZB6V4FTMzZdBRomHOquBy89BjC1Hz ksP8bsDnMPPpJ2KYNFFlA== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.81 tagged_above=-5 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_NONE=-0.0001, T_FRT_CONTACT=0.01 X-Spam-Level: X-Archive-Number: 201006/18 X-Sequence-Number: 34209 In response to Wes James : > In the statement: > > select > MAX(page_count_count) - MIN(page_count_count) as day_tot, > MAX(page_count_count) as day_max, sum(MAX(page_count_count) - > MIN(page_count_count)) as tot, > page_count_pdate > from page_count > group by page_count_pdate order by page_count_pdate > > Is there a way to do sum(day_tot) also in the same statement? Is this the correct table-definition? test=# \d page_count Table "public.page_count" Column | Type | Modifiers ------------------+---------+----------- page_count_pdate | date | page_count_count | integer | If yes, your SQL is wrong: test=# select MAX(page_count_count) - MIN(page_count_count) as day_tot, MAX(page_count_count) as day_max, sum(MAX(page_count_count) - MIN(page_count_count)) as tot, page_count_pdate from page_count group by page_count_pdate order by page_count_pdate; ERROR: aggregate function calls cannot be nested LINE 3: MAX(page_count_count) as day_max, sum(MAX(page_count_cou... Can you provide the correct table definition? Regards, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99