in reply to DBI, SQL query problem

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.

Replies are listed 'Best First'.
Re^2: DBI, SQL query problem
by sirius98 (Acolyte) on Dec 26, 2004 at 00:45 UTC
    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