http://qs321.pair.com?node_id=778479

CG_man has asked for the wisdom of the Perl Monks concerning the following question:

I've tried many different things on this question that I found on different forums and nothing seems to work. My question is how can you remove duplicate entries from a SQL query in a perl script, where it keeps the newest entry? Right now the results are as follows from the query;

Jobname status Comment Reason
------- ------ ------- ------
job A HOLD 12:35 per test
job B HOLD 1:45 per user
job C HOLD 3:00 maint
job C HOLD 3:30 per Failure

I need the results to only give me the newest job C and not both.
Here is my perl script

#!/users/contrib/bin/perl

use Sybase::Sybperl;
use Env qw(BATCH_USER_NAME BATCH_PASSWORD);

##############
############## Subroutines
##############

# Send input string to the DB server and get the results
# $_[0] - sql string
sub DBEXEC {
&dbcmd($dbproc, $_[0]);
&dbsqlexec($dbproc);
&dbresults($dbproc);
}
# Turn UTC in to people time
sub GETTIME {
if ($_[0] eq "999999999") {
$time = ("******** **********");
} else {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($_[0]);
$mon++;
$time = sprintf ("%.2d:%.2d:%.2d %.2d/%.2d/%.2d", $hour, $min, $sec, $mon, $mday, $year + 1900);
}
}
# set job status
sub JOBSTATUS {
if ($_[0] == 9) {$jstatus1 = ("ACTIVATED");}
elsif ($_[0] == 5) {$jstatus2 = ("FAILURE");}
elsif ($_[0] == 8) {$jstatus3 = ("INACTIVE");}
elsif ($_[0] == 11) {$jstatus4 = ("ON_HOLD");}
elsif ($_[0] == 7) {$jstatus5 = ("ON_ICE");}
elsif ($_[0] == 12) {$jstatus5 = ("QUE_WAIT");}
elsif ($_[0] == 10) {$jstatus6 = ("RESTART");}
elsif ($_[0] == 1) {$jstatus7 = ("RUNNING");}
elsif ($_[0] == 3) {$jstatus8 = ("STARTING");}
elsif ($_[0] == 4) {$jstatus9 = ("SUCCESS");}
elsif ($_[0] == 6) {$jstatus10 = ("TERMINATED");}
}

# set event status
sub EVENTSTCODE {
if ($_[0] == 110) {$jstatus1 = ("JOB_ON_ICE");}
elsif ($_[0] == 112) {$jstatus2 = ("JOB_ON_ICE");}
elsif ($_[0] == 106) {$jstatus3 = ("ALARM");}
elsif ($_[0] == 108) {$jstatus4 = ("FORCE_START");}
elsif ($_[0] == 105) {$jstatus5 = ("KILLJOB");}
elsif ($_[0] == 111) {$jstatus5 = ("JOB_OFF_ICE");}
elsif ($_[0] == 113) {$jstatus6 = ("JOB_OFF_HOLD");}
elsif ($_[0] == 117) {$jstatus7 = ("COMMENTS");}

}

format STDOUT6 =
@<<<< @<<<< @<<<< @<<<<
$name, $jstatus, $estamp, $etext
.

$reporttime = &GETTIME(time);

# login to the database
$dbproc = &dblogin(USER, PSSWD, 'SQL_DEV_SERVER');

############
############ Loop for job status check on HOLD.
############
foreach $statusnumber (11) {
foreach $eventnumber (117) {
$statusname = &JOBSTATUS($statusnumber);
$eventstatus = &EVENTSTCODE($eventnumber);
# Submit SQL Statement
$~ = STDOUT6;
print "Jobs on HOLD w/comments last Issued for job \n";
print " \n";
print "Jobname Status Comment reason\n";
print "------- ------ ------- ---------\n";
&DBEXEC("select proc_eventvu.job_name, jobst.status, proc_eventvu.stamp, proc_eventvu.text from proc_eventvu, jobst where proc_eventvu.joid = jobst.joid and proc_eventvu.event in (112,$eventnumber) and jobst.status = $statusnumber order by job_name");
while(@status = &dbnextrow($dbproc)){
$name = ($status[0]);
$jstatus = &JOBSTATUS($status1);
$estamp = ($status2);
$etext = ($status3);

write;}
print " \n";
print " \n";
}
}

I'd appreciate any help, code, and explination so I can understand what I am missing