From: Tom Lane <tgl@sss.pgh.pa.us>
To: Collin Peters <cadiolis@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Am I crazy or is this SQL not possible
Date: Thu, 01 Jun 2006 15:21:31 -0400
Message-ID: <14505.1149189691@sss.pgh.pa.us> (raw)
In-Reply-To: <df01c91b0606011043g61328d6habaee8fa037a5ccf@mail.gmail.com>
References: <df01c91b0606011043g61328d6habaee8fa037a5ccf@mail.gmail.com>
"Collin Peters" <cadiolis@gmail.com> writes:
> What I would like to do is simply get the last date_sent and it's
> status for every broadcast. I can't do a GROUP BY because I can't put
> an aggregate on the status column.
You missed the key idea about how to use DISTINCT ON.
SELECT DISTINCT ON (email_broadcast_id) *
FROM email_broadcast_history
ORDER BY email_broadcast_id, date_sent DESC
You order by the DISTINCT ON fields, then one or more additional fields
to select the representative row you want within each DISTINCT ON group.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, cadiolis@gmail.com
Subject: Re: Am I crazy or is this SQL not possible
In-Reply-To: <14505.1149189691@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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