in reply to DBI, SQL query problem

You're wanting to check out the DBI module to get your connection to the database. Here is a simple way of going about it; I'm sure you can do the differencing within the SQL, but I never did go and learn SQL functions :). So this is one way of going about it:

=for SQL Table ------------------------------------ | theIDNum | theDate | thePrice | ------------------------------------ | ax1 | 2004-12-01 | 300.00 | | ax2 | 2004-12-01 | 500.00 | | ax1 | 2004-12-02 | 200.00 | | ax1 | 2004-12-03 | 100.00 | | ax3 | 2004-12-01 | 300.00 | | ax2 | 2004-12-01 | 400.00 | ------------------------------------ =cut use strict; use DBI; my $dbh = DBI->connect('dbi:mysql:database', 'u', 'p', { RaiseError => 1, AutoCommit => 1 } ); my $rows = $dbh->selectall_arrayref( 'SELECT thePrice FROM theTable ORDER BY ' . 'theDate DESC, thePrice LIMIT 2' ); my $diff = abs( $rows->[0][0] - $rows->[1][0] ); printf("%s%.2f\n", 'Difference of $', $diff); __END__ Difference of $50.00

Replies are listed 'Best First'.
Re^2: DBI, SQL query problem
by sirius98 (Acolyte) on Dec 25, 2004 at 03:40 UTC
    Sorry I didnt not correctly state the problem