use strict; use warnings; use DBI; unlink 'db.SQLite'; # Build the database my $dbh = DBI->connect ("dbi:SQLite:dbname=db.SQLite","",""); $dbh->do ('CREATE TABLE employees (employee TEXT, form TEXT, date TEXT)'); my $sth = $dbh->prepare ('INSERT INTO employees (employee, form, date) VALUES (?, ?, ?)'); $sth->execute (do {chomp; split}) while ; print "Access by employee\n"; $sth = $dbh->prepare ( 'SELECT * FROM employees ORDER BY employee, form, date' ); $sth->execute (); my $employee = ''; while (my $row = $sth->fetchrow_hashref ()) { if ($employee ne $row->{employee}) { $employee = $row->{employee}; print "$employee\n"; } printf " %-6s %s\n", @{$row}{qw(form date)}; } print "Access by form\n"; $sth = $dbh->prepare ( 'SELECT * FROM employees ORDER BY form, employee, date' ); $sth->execute (); my $form = ''; while (my $row = $sth->fetchrow_hashref ()) { if ($form ne $row->{form}) { $form = $row->{form}; print "$form\n"; } printf " %-8s %s\n", @{$row}{qw(employee date)}; } __DATA__ 10001 10 20090101 10002 10 20080515 10003 10 20090323 10001 20 20090412 10002 20 20090711 #### Access by employee 10001 10 20090101 20 20090412 10002 10 20080515 20 20090711 10003 10 20090323 access by form 10 10001 20090101 10002 20080515 10003 20090323 20 10001 20090412 10002 20090711