Subject | Re: select query |
---|---|
Author | petr.jakes |
Post date | 2010-01-31T17:01:55Z |
>Set,
> 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
>
I am just saying WOW. I was never thinking about it this way.
Thanks.
Petr