Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 963E963249C for ; Fri, 4 Jun 2010 10:44:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 62050-03 for ; Fri, 4 Jun 2010 13:44:25 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from jupiter.marktest.pt (delta.marktest.pt [80.251.174.116]) by mail.postgresql.org (Postfix) with ESMTP id 5664C632EB7 for ; Fri, 4 Jun 2010 10:44:27 -0300 (ADT) Received: from Proteu (proteu.marktest.pt [10.61.90.54]) by jupiter.marktest.pt with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id LJLCF70F; Fri, 4 Jun 2010 14:44:24 +0100 Message-ID: From: "Oliveiros d'Azevedo Cristina" To: "Wes James" , References: <20100604055434.GA19411@a-kretschmer.de> Subject: Re: sum an alias Date: Fri, 4 Jun 2010 14:44:24 +0100 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5512 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.229 tagged_above=-5 required=5 tests=BAYES_50=0.8, STOX_REPLY_TYPE=0.439, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: * X-Archive-Number: 201006/21 X-Sequence-Number: 34212 ----- Original Message ----- From: "Wes James" To: Sent: Friday, June 04, 2010 2:30 PM Subject: Re: [SQL] sum an alias On Thu, Jun 3, 2010 at 11:54 PM, A. Kretschmer wrote: > 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? > You can use a nested SELECT. Is there some reason preventing you from doing that? Why don't you do something like SELECT SUM(day_tot) FROM ( select MAX(page_count_count) - MIN(page_count_count) as day_tot, MAX(page_count_count) as day_max, page_count_pdate from page_count group by page_count_pdate order by page_count_pdate ); Maybe I 'm misunderstanding the background of what you want to do Best, Oliveiros