Re: getting maximum entry from a sum() - Mailing list pgsql-novice

From Tom Lane
Subject Re: getting maximum entry from a sum()
Date
Msg-id 20747.1160146182@sss.pgh.pa.us
Whole thread Raw
In response to getting maximum entry from a sum()  (Jan Danielsson <jan.danielsson@gmail.com>)
List pgsql-novice
Jan Danielsson <jan.danielsson@gmail.com> writes:
>    Essentially, I want:

> select dt,sum(amount) as asum where asum=(select max(asum) ...) group by dt

There are a couple ways you could do it:

* HAVING clause:

select dt,sum(amount) as asum
  group by dt
  having sum(amount) = (select max(asum) ...)

* ORDER BY/LIMIT:

select dt,sum(amount) as asum
  group by dt
  order by asum desc
  limit 1

The first is standard SQL, the second isn't (no LIMIT in the spec)
but the second is probably more efficient.

            regards, tom lane

pgsql-novice by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: getting maximum entry from a sum()
Next
From: Darren R
Date:
Subject: Re: postgresql db account