pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedsum an alias
4+ messages / 3 participants
[nested] [flat]
* sum an alias
@ 2010-06-04 02:16 Wes James <comptekki@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Wes James @ 2010-06-04 02:16 UTC (permalink / raw)
To: pgsql-sql
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?
thx,
-wes
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: sum an alias
@ 2010-06-04 05:54 A. Kretschmer <andreas.kretschmer@schollglas.com>
parent: Wes James <comptekki@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: A. Kretschmer @ 2010-06-04 05:54 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: sum an alias
@ 2010-06-04 13:30 Wes James <comptekki@gmail.com>
parent: A. Kretschmer <andreas.kretschmer@schollglas.com>
0 siblings, 1 reply; 4+ messages in thread
From: Wes James @ 2010-06-04 13:30 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: sum an alias
@ 2010-06-04 13:44 Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>
parent: Wes James <comptekki@gmail.com>
0 siblings, 0 replies; 4+ messages in thread
From: Oliveiros d'Azevedo Cristina @ 2010-06-04 13:44 UTC (permalink / raw)
To: Wes James <comptekki@gmail.com>; pgsql-sql
----- Original Message -----
From: "Wes James" <comptekki@gmail.com>
To: <pgsql-sql@postgresql.org>
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
<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?
>
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
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2010-06-04 13:44 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-06-04 02:16 sum an alias Wes James <comptekki@gmail.com>
2010-06-04 05:54 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2010-06-04 13:30 ` Wes James <comptekki@gmail.com>
2010-06-04 13:44 ` Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>
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