Re: sum an alias

From: Wes James <comptekki(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: sum an alias
Date: 2010-06-04 13:30:18
Message-ID: AANLkTilYVcqa-2exe48IhvV-HSEcWKFSAO1B03hsoBtG@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Oliveiros d'Azevedo Cristina 2010-06-04 13:44:24 Re: sum an alias
Previous Message Jasen Betts 2010-06-04 10:01:56 Re: how to construct sql