Skip site navigation (1) Skip section navigation (2)

Re: Select maximum amoung grouped sums

From: Jan Danielsson <jan(dot)m(dot)danielsson(at)gmail(dot)com>
To: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Select maximum amoung grouped sums
Date: 2007-02-07 19:16:59
Message-ID: 45CA25AB.2070809@gmail.com (view raw or flat)
Thread:
Lists: pgsql-novice
Richard Broersma Jr wrote:
> --- Jan Danielsson <jan(dot)m(dot)danielsson(at)gmail(dot)com> wrote:
>> ----------------------
>> select date_part('year', dt) as year, date_part('week', dt) as week,
>> 	sum(amount) as asum
>> from transactions
>> group by year,week
>   having asum = (
>                  select max(A.weeksum)
>                  from
>                      (
>                       select sum(amount) as weeksum
>                       from transactions
>                       group by date_trunc('week', dt)
>                      ) A
>                 )
>> order by year,week;
>> ----------------------
> 
> 
> This should do what you want, but I expect that there is a better answer.

   Hmm... This gives me:

ERROR:  column "asum" does not exist
LINE 5: having asum = (

   Any ideas?

-- 
Kind regards,
Jan Danielsson


In response to

Responses

pgsql-novice by date

Next:From: Cyrus DowneyDate: 2007-02-07 19:22:19
Subject: Re: Select maximum amoung grouped sums [VASCL:A11557D9645]
Previous:From: Richard Broersma JrDate: 2007-02-07 16:09:09
Subject: Re: Select maximum amoung grouped sums

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group