Re: Am I crazy or is this SQL not possible

From: "Klay Martens" <kmartens(at)wol(dot)co(dot)za>
To: patrick(dot)jacquot(at)anpe(dot)fr, cadiolis(at)gmail(dot)com
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Am I crazy or is this SQL not possible
Date: 2006-06-02 11:53:42
Message-ID: 6c800ab64b48492fa88fa92f4fc28625@wol.co.za
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

<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(at)gmail(dot)com; <BR>&nbsp;<B>Cc: </B>pgsql-sql(at)postgresql(dot)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>

Attachment Content-Type Size
unknown_filename text/html 2.7 KB

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Aaron Bono 2006-06-02 15:23:30 Re: Am I crazy or is this SQL not possible
Previous Message Patrick Jacquot 2006-06-02 08:45:35 Re: Am I crazy or is this SQL not possible