Subject Re: select query
Author petr.jakes
>
> Hi Petr,
> assuming you want the latest record for each PERSON_ID rather than NAME (there may be two persons with the same name), I'd say you're simply looking for something like
>
> SELECT P.NAME, M.SUBJECT, M.TIMESTAMP
> FROM PERSON p
> JOIN MESSAGE m ON p.ID = m.PERSON_ID
> WHERE NOT EXISTS
> (SELECT *
> FROM MESSAGE m2
> WHERE m2.PERSON_ID = m.PERSON_ID
> AND m2.TIMESTAMP > m.TIMESTAMP)
>
> Alternatively, you can use this (same select as above, just written a bit differently):
>
> SELECT P.NAME, M.SUBJECT, M.TIMESTAMP
> FROM PERSON p
> JOIN MESSAGE m ON p.ID = m.PERSON_ID
> LEFT JOIN MESSAGE m2 ON p.ID = m2.PERSON_ID
> AND m.TIMESTAMP < m2.TIMESTAMP
> WHERE m2.ID IS NULL
>
> HTH,
> Set
>

Set,
I am just saying WOW. I was never thinking about it this way.
Thanks.
Petr