Subject SQL Puzzle
Author James
Iam using FB 1.0.3

Iam having problem constructing this query, wherein I will be using
three tables. One is item, invoice, and invoicedetails. What I want to
achieve is that all the items in the item table will get the last price
information at the invoice and invoicedetails. In invoice table is where
we could find which is the lastest purchase for a particular item and in
the invoicedetails table is where we get the price.

Iam trying to avoid using SP here/but if I got no choice then I think I
have to do it in SP.

so far here is what I have.

select m.ID, m."Name", m3."InvNo", m3."InvDate", m2."Price"
from "items" m left join "invoicedetails" m2 on m.ID = m2."ItemID"
left join "invoice" m3 on m2.ID = m3.ID
where exists (select max(d2."InvDate") from "invoicedetails" d left join
"invoice" d2 on d.ID = d2.id
where m3.ID = d2.ID and m3."InvDate" = d2."InvDate"
group by d."ItemID")

but this is all wrong ... this query still giving me the list of all
items purchase and not the lastest only, which is what I need.

Cheers,
James