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.
Next, you say the price difference is what you want, so:from table a join table b on -- the primary key where a.idNumber = b.idNumber
And finally, you want the two most recent items, which should be something like:select abs(a.price - b.price) as difference
And if you put it all together, you get something like this: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)
That may or may not work, depending on your data model. But it should get you pointed in the right direction.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)
In reply to Re: DBI, SQL query problem
by VSarkiss
in thread DBI, SQL query problem
by sirius98
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |