pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Klay Martens <kmartens@wol.co.za>
To: patrick.jacquot@anpe.fr
To: cadiolis@gmail.com
Cc: pgsql-sql@postgresql.org
Subject: Re: Am I crazy or is this SQL not possible
Date: Fri, 02 Jun 2006 13:53:42 +0200
Message-ID: <6c800ab64b48492fa88fa92f4fc28625@wol.co.za> (raw)

<BR>Sorry to stick my nose in here...<BR>would not this work better?<BR><BR>SELECT broadcast_id,date_sent,status from broadcast_history where<BR>unique_id in (<BR>SELECT max(unique_id) from broadcast_history group by broadcast_id);<BR><BR>Seems like a simpler option.<BR><BR>
<P>&nbsp;<FONT size=2>----- Original Message ------<BR>&nbsp;<B>From:</B>Patrick Jacquot<BR>&nbsp;<B>Sent:</B>Friday, June 02, 2006 10:45<BR>&nbsp;<B>To:</B>Collin Peters cadiolis@gmail.com; <BR>&nbsp;<B>Cc: </B>pgsql-sql@postgresql.org; <BR>&nbsp;<B>Subject:</B>Re: [SQL] Am I crazy or is this SQL not possible</FONT>
<P>
<TABLE>
<TBODY>
<TR>
<TD>
<META content="text/html; charset=windows-874" http-eqviv="Content-Type"><FONT size=2>Collin Peters wrote:<BR><BR>&gt; I am having some serious mental block here. Here is the abstract<BR>&gt; version of my problem. I have a table like this:<BR>&gt;<BR>&gt; unique_id (PK) broadcast_id date_sent status<BR>&gt; 1 1 2005-04-04 30<BR>&gt; 2 1 2005-04-01 30<BR>&gt; 3 1 2005-05-20 10<BR>&gt; 4 2 2005-05-29 30<BR>&gt;<BR>&gt; So it is a table that stores broadcasts including the broadcast_id,<BR>&gt; the date sent, and the status of the broadcast.<BR>&gt;<BR>&gt; What I would like to do is simply get the last date_sent and it's<BR>&gt; status for every broadcast. I can't do a GROUP BY because I can't put<BR>&gt; an aggregate on the status column.<BR>&gt;<BR>&gt; SELECT MAX(date_sent), status<BR>&gt; FROM broadcast_history<BR>&gt; GROUP BY broadcast_id<BR>&gt;<BR>&gt; How do I get the status for the most recent date_sent using GROUP BY?<BR>&gt;<BR>&gt; DISTINCT also doesn't work<BR>&gt;<BR>&gt; SELECT DISTINCT ON (email_broadcast_id) *<BR>&gt; FROM email_broadcast_history<BR>&gt; ORDER BY date_sent<BR>&gt;<BR>&gt; As you have to have the DISTINCT fields matching the ORDER BY fields.<BR>&gt; I have to ORDER BY date_sent, but I can't do a DISTINCT on date_sent<BR>&gt;<BR>&gt; I keep thinking am I missing something. Does anybody have any ideas?<BR>&gt;<BR>&gt; ---------------------------(end of broadcast)---------------------------<BR>&gt; TIP 2: Don't 'kill -9' the postmaster<BR>&gt;<BR>You can also try<BR>SELECT * from broadcast_history A WHERE NOT EXISTS<BR>(SELECT * from broadcast_history B WHERE B.date_sent &gt;A.date_sent)<BR>There isn't any PostgreSQL-ism, just a correlated subrequest wich is <BR>perfectly standars, afaik<BR><BR><BR>&nbsp;&nbsp;&nbsp;---------------------------(end of broadcast)---------------------------<BR>&nbsp;&nbsp;&nbsp;TIP 9: In versions below 8.0, the planner will ignore your desire to<BR>&nbsp;&nbsp;&nbsp; choose an index scan if your joining column's datatypes do not<BR>&nbsp;&nbsp;&nbsp; match<BR>&nbsp;&nbsp;&nbsp;<BR></FONT></TD></TR></TBODY></TABLE></P>




view thread (20+ messages)  latest in thread

Message-ID: <6c800ab64b48492fa88fa92f4fc28625@wol.co.za>
Permalink:  ../6c800ab64b48492fa88fa92f4fc28625@wol.co.za/
Also on:    postgresql.org/message-id/6c800ab64b48492fa88fa92f4fc28625@wol.co.za

 · 

reply

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: kmartens@wol.co.za, patrick.jacquot@anpe.fr, cadiolis@gmail.com
  Subject: Re: Am I crazy or is this SQL not possible
  In-Reply-To: <6c800ab64b48492fa88fa92f4fc28625@wol.co.za>

* 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