For example, if one key has two check ins or two check outs in a row, it would be flagged. I'm really not sure how to do this. Any ideas?
It looks likely your table structure is not ideal but if you are stuck with it you can use user defined variables to hold previous record values
SELECT key_number, dateadded,
IF (action=@last_action AND key_number=@last_key,"ERROR","OK"),
@last_action := action, @last_key := key_number
FROM ( SELECT *
FROM keysafe
ORDER BY key_number, dateadded
) as x
Initialise the variables before use with
$dbh->do('SET @last_action := "UNKNOWN",@last_key="UNKNOWN"');
alternatively use perl variables
my $sql = 'SELECT key_number,action,dateadded
FROM keysafe
ORDER BY key_number, dateadded';
my $sth = $dbh->prepare($sql);
$sth->execute();
my ($last_key,$last_action);
while (my ($key,$action,$date) = $sth->fetchrow_array){
my $status = 'OK';
if (($key eq $last_key) && ($action eq $last_action)){
$status = 'ERROR';
};
print "$key,$action,$date,$status\n";
$last_key = $key;
$last_action = $action;
}
poj
|