Re: Best way to do this query?

From: Tonkuma <tonkuma_at_jp.ibm.com>
Date: 15 Jan 2007 22:27:59 -0800
Message-ID: <1168928879.693739.124840_at_q2g2000cwa.googlegroups.com>


Cimode wrote:
> Marshall wrote:
>
> > But suppose I also want to know the id and amount of that particular
> > invoice?
> Something like this should do...
>
> select * from Invoices A
> inner join
> (
> select invoiceid, max(date) as date1 from Invoices group by customerid
> ) B
> on A.invoiceid = B.invoiceid and
> A.date = B.date1
>
> Hope this helps...
Cimode, you should use customerid instead of invoiceid for JOIN condition.
SELECT I.*
  FROM Invoices I

       INNER JOIN
       (SELECT customerid, MAX(date) most_recent_date
          FROM Invoices
         GROUP BY customerid
       ) AS C
         ON  I.customerid = C.customerid
         AND I.date = C.most_recent_date
Received on Tue Jan 16 2007 - 07:27:59 CET

Original text of this message