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

Merry Christmas,

My data looks like this:

({idNumber,Date,Price})=({ax1,1/12/04,$300},
{ax2,1/12/04,$500},{ax1,2/12/04,$200},{ax1,3/12/04,$100},
{ax3,1/12/04,$300},{ax2,1/12/04,$400})

EDITED My problem is i need to find the price difference between
the two most recent items of any items with the same id number.

Im using DBI the driver is ODBC and the database Access.

I was hoping to do mosy of the processing in SQL.

Any Help would be great Pete

Replies are listed 'Best First'.
Re: DBI, SQL query problem
by saskaqueer (Friar) on Dec 25, 2004 at 02:59 UTC

    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
      Sorry I didnt not correctly state the problem
Re: DBI, SQL query problem
by VSarkiss (Monsignor) on Dec 25, 2004 at 20:06 UTC

    This really isn't a Perl question, it's a SQL question.

    SQL is a declarative language, which means you describe what you want, rather than how to get it. You've stated you want to compare things in the same table, so you'll need aliases.

    from table a join table b on -- the primary key where a.idNumber = b.idNumber
    Next, you say the price difference is what you want, so:
    select abs(a.price - b.price) as difference
    And finally, you want the two most recent items, which should be something like:
    where a.date = (select max(Date) from table t1 where t1.idNumber = a.idNumber) and b.Date = (select max(Date) from table t2 where t2.idNumber = b.idNumber)
    And if you put it all together, you get something like this:
    select a.idNumber -- Presuming you want , b.idNumber -- these too. , abs(a.Price - b.Price) -- the price difference from table a join table b on SOMETHING -- the primary key where a.idNumber = b.idNumber and a.date = (select max(Date) from table t1 where t1.idNumber = a.idNumber) and b.Date = (select max(Date) from table t2 where t2.idNumber = b.idNumber)
    That may or may not work, depending on your data model. But it should get you pointed in the right direction.

      Thanks alot i made a bit of a work around but got that to work. I have a futher SQL problem but i will address that in a more appropriate area, thanks again
Re: DBI, SQL query problem
by thor (Priest) on Dec 25, 2004 at 15:35 UTC
    The problem is that the RDBMS model isn't really designed for what you're trying to accomplish. I've done things like this in the past with a cursor, which may or may not be appropriate here. Why the (seemingly arbitrary) restriction of having to do this all server side? With a programming language, you can do this quite easily...

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

Re: DBI, SQL query problem
by aquarium (Curate) on Dec 25, 2004 at 21:28 UTC
    as per previous answers, they should get you almost there. what i'm concerned about (warning lights/sounds) is that your data model is not normalised, which makes the SQL harder to figure out than it ought to be. it also makes the SQL very dependent on some intrinsic nature of your data. I could be wrong of couse -- but then state what's the primary key of this table? The table looks harmless enough; but it will keep biting you back if it's not normalised.
    the hardest line to type correctly is: stty erase ^H