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.


In reply to Re: DBI, SQL query problem by VSarkiss
in thread DBI, SQL query problem by sirius98

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.