Selecting All Columns Associated With Maximum Value of One Column

From: Rich Shepard <rshepard(at)appl-ecosys(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Selecting All Columns Associated With Maximum Value of One Column
Date: 2011-10-05 23:34:49
Message-ID: alpine.LNX.2.00.1110051628280.25250@salmo.appl-ecosys.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

A table (chemistry) has columns named site_id, sample_date, param, quant,
and str_name (among other columns). I want to find the site_id, sample_date,
and quant for a specific str_name and param. I cannot get the proper syntax
in the SELECT statement.

My attempts are variations of,

SELECT max(quant), param, site_id, sample_date, str_name from chemistry
WHERE param = 'TDS' AND str_name = 'BurrowCrk';

which prompts postgres to tell me,

ERROR: column "chemistry.param" must appear in the GROUP BY clause or be
used in an aggregate function

I suspect that retrieving these data requires nested SELECT statements,
and I'd appreciate learning how to retrive such data.

Rich

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2011-10-05 23:58:34 Re: Selecting All Columns Associated With Maximum Value of One Column
Previous Message Leif Biberg Kristensen 2011-10-05 22:43:31 Re: I/O error on data file, can't run backup