Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 177A3633663 for ; Fri, 4 Jun 2010 10:30:28 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 32025-08 for ; Fri, 4 Jun 2010 13:30:20 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pz0-f176.google.com (mail-pz0-f176.google.com [209.85.222.176]) by mail.postgresql.org (Postfix) with ESMTP id A6AEA6335C2 for ; Fri, 4 Jun 2010 10:30:20 -0300 (ADT) Received: by pzk6 with SMTP id 6so613138pzk.1 for ; Fri, 04 Jun 2010 06:30:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=dlctJ06JDPvx6uaqIdCDXXB+hAUTLwaaHBrj3l38Qfc=; b=m1n+5Q6VaBMW96bzPlf6Wsb5OblJkzoUEPfQPa9aHzC0zlUHVVjEBgzRJgNFs+tKNL QTvPRBqURLJpWTmQkBlqqChI4xvPN+yff1ya79P73dLUFHIJVxeyycS5Lf31jdLI+5iA 2wq9zfbow/qOJyffKO4f9ICLusfoM6SbPN4SU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=ZL+HN4NqfakmrVC5YnZc1EJJWK2NAT2NXnZl5ikIv6nZrNkve81bq3NuVsISJi3Rad aP8+Scmt+hKE7yyM4MdGkVJpi4Pxb35Yh+D4ypEQ9Yom+G+CqkpvHNjjozjcwXIFFPOG QDfW24ZXM+mZsRQnwobWTGQxc4bGBo5lDxNO0= MIME-Version: 1.0 Received: by 10.140.55.11 with SMTP id d11mr8958138rva.254.1275658218474; Fri, 04 Jun 2010 06:30:18 -0700 (PDT) Received: by 10.141.40.21 with HTTP; Fri, 4 Jun 2010 06:30:18 -0700 (PDT) In-Reply-To: <20100604055434.GA19411@a-kretschmer.de> References: <20100604055434.GA19411@a-kretschmer.de> Date: Fri, 4 Jun 2010 07:30:18 -0600 Message-ID: Subject: Re: sum an alias From: Wes James To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.5 tagged_above=-5 required=5 tests=BAYES_05=-0.5, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201006/20 X-Sequence-Number: 34211 On Thu, Jun 3, 2010 at 11:54 PM, A. Kretschmer wrote: > In response to Wes James : >> In the statement: >> >> select >> =A0 =A0 MAX(page_count_count) - MIN(page_count_count) as day_tot, >> =A0 =A0 MAX(page_count_count) as day_max, sum(MAX(page_count_count) - >> MIN(page_count_count)) as tot, >> =A0 =A0 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=3D# \d page_count > =A0 =A0 =A0 Table "public.page_count" > =A0 =A0 =A0Column =A0 =A0 =A0| =A0Type =A0 | Modifiers > ------------------+---------+----------- > =A0page_count_pdate | date =A0 =A0| > =A0page_count_count | integer | That is the correct table def. > If yes, your SQL is wrong: I know the sql is wrong - I should have mentioned that, but I'm looking for something that will work like that. I need to sum the difference of max() - min((). How can this be done, since there doesn't seem to be a way to sum aggregates. > > test=3D# select > =A0 =A0MAX(page_count_count) - MIN(page_count_count) as day_tot, > =A0 =A0MAX(page_count_count) as day_max, sum(MAX(page_count_count) - > MIN(page_count_count)) as tot, > =A0 =A0page_count_pdate > from page_count > group by page_count_pdate order by page_count_pdate; > ERROR: =A0aggregate function calls cannot be nested > LINE 3: =A0 =A0 MAX(page_count_count) as day_max, sum(MAX(page_count_cou.= .. > > > Can you provide the correct table definition? thx, -wes