If anyone is interested, here is the code we eventually have used: <?php $sSql = "select id from crm_leads "; $sLeads = pg_exec($conn,$sSql); for($e=0;$e<pg_numrows($sLeads);$e++){ $sSql = "SELECT * FROM crm_events where crm_leads_id = ".pg_result($sLeads,$e,"id")." and follow_up_action!='' ORDER BY follow_up_date desc limit 1"; $sRes = pg_exec($conn,$sSql); $sHits = pg_numrows($sRes); if($sHits!=0){ for($i=0;$i<$sHits;$i++){ $temp_date = explode("-",pg_result($sRes,$i,"follow_up_date")); $temp_tstamp = gmmktime(0,0,0,$temp_date[1],$temp_date[2],$temp_date[0]); $array_echo[$temp_tstamp] .= "<tr><td>".$compName1."</td><td>".$empl1."</td><td>".pg_result($sRes,$i,"date")."</td><td>".pg_result($sRes,$i,"description")."</td><td>".pg_result($sRes,$i,"type")."</td><td>".pg_result($sRes,$i,"follow_up_action")."</td><td>".pg_result($sRes,$i,"follow_up_date")."</td><td><a href=''>Edit</a></td><td><a href=''>Action</a></td></tr>"; } } } ?> <html> <head> <title> CRM - Events </title> </head> <body bgcolor="<?echo $sBodyColor?>"> <?php require("menu.php"); ?> <table width='100%'> <tr><td><h2><font color='<? echo $sHeadingColor?>'>Events Requiring Action</font></h2></td> <td align='right'><a href='' target='_blank'><img src='' border=0></a></td> </table> <? asort($array_echo); ?> <? foreach($array_echo as $ae){ echo $ae; } ?> <? echo $prog_echo_end; ?> </body> </html> The limit 1 gets us the last qualifying event on each lead, and this result is stuck into an array which is sorted by follow_up_date prior to display. I probably didn't explain the problem too well! But thanks for the input. Andy On Wed, 2006-05-03 at 08:56 -0600, Sherwin M. Harris wrote: Assuming you are using a version of a database that can support sub-queries (MySQL 4.1 and up, Postgresql (all reasonably current versions, any other robust RDBMS) you can do the query like this: Sherwin Harris From:pgsql-php-owner@xxxxxxxxxxxxxx [mailto:pgsql-php-owner@xxxxxxxxxxxxxx] On Behalf Of Andy Dunlop
|