Re: Multiple columns w/single aggregate

From: Ian Harding <iharding(at)pakrat(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Multiple columns w/single aggregate
Date: 2001-01-21 22:05:36
Message-ID: 3A6B5D30.FFFA4E4E@pakrat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Robert M. Craver II" wrote:

> I cannot successfully execute the sql statement:
>
> SELECT percentage, MAX(start_date) FROM sometable
> WHERE id = 1
>
> I get the error message:
>
> ERROR: Attribute sometable.percentage must be GROUPed
> or used in an aggregate function
>
> I know that I have seen examples of selecting multiple columns with a
> single colunn aggregate function. In fact, the new 'PostgreSQL:
> Introductions and Concepts' book by Bruce Momjian has two on page 53.
> Does anyone know why this doesn't work?
>
> Thanks in advance.

True, but you need to group by the non-aggregate columns. In your case,
just add

GROUP BY percentage

This will give you a listing of all distinct 'percentage' values, and
the maximum of their 'start_date' values, that have id=1. It will blow
up if there are any NULL in 'percentage' unlike some inferior products
that simply silently eliminate them for you.

Ian

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Massimo Di+Pierro 2001-01-21 22:05:42 cannot create init file mydb/base/mydb/pg_internal.init.1016
Previous Message Nic Ferrier 2001-01-21 21:55:37 Re: creating groups (and list archives broken?)