pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Wes James <comptekki@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: sum an alias
Date: Fri, 4 Jun 2010 07:30:18 -0600
Message-ID: <AANLkTilYVcqa-2exe48IhvV-HSEcWKFSAO1B03hsoBtG@mail.gmail.com> (raw)
In-Reply-To: <20100604055434.GA19411@a-kretschmer.de>
References: <AANLkTil0i1cg1DulN4sIEIa08qM5VU5VuLoKB7u8g1-D@mail.gmail.com>
	<20100604055434.GA19411@a-kretschmer.de>

On Thu, Jun 3, 2010 at 11:54 PM, A. Kretschmer
<andreas.kretschmer@schollglas.com> 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?
>
> 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 |


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=# 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?


thx,

-wes



view thread (4+ messages)  latest in thread

Message-ID: <AANLkTilYVcqa-2exe48IhvV-HSEcWKFSAO1B03hsoBtG@mail.gmail.com>
Permalink:  ../AANLkTilYVcqa-2exe48IhvV-HSEcWKFSAO1B03hsoBtG@mail.gmail.com/
Also on:    postgresql.org/message-id/AANLkTilYVcqa-2exe48IhvV-HSEcWKFSAO1B03hsoBtG@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: comptekki@gmail.com
  Subject: Re: sum an alias
  In-Reply-To: <AANLkTilYVcqa-2exe48IhvV-HSEcWKFSAO1B03hsoBtG@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox