List:General Discussion« Previous MessageNext Message »
From:Peter Brawley Date:November 16 2006 4:51pm
Subject:Re: How do I do this query efficiently?
View as plain text  
Michael,

 >SELECT t1.id, t1.version, t1.value
 >FROM data t1
 >LEFT JOIN data t2 ON t1.id=t2.id AND t1.version < t2.version
 >WHERE t2.id IS NULL;
 >I had almost the same problem and I found this solution very smart...
 >even smarter than I can understand :)
 >Can someone explain to me why/how this query works ?

A two-step:
1. The left join creates a logical table with t1.ids on the left, and on 
the right ...
    (a) matching t2.ids, when t1.version<t2.version, or
    (b) nulls when t1.version>=t2.version.
2. The where clause removes (a), leaving only the t1.ids for which 
there's no greater version.

PB


-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.409 / Virus Database: 268.14.6/536 - Release Date: 11/16/2006

Thread
How do I do this query efficiently?Sebastiaan van Erk13 Nov
  • Re: How do I do this query efficiently?Peter Brawley13 Nov
    • Re: How do I do this query efficiently?Rolando Edwards13 Nov
      • Re: How do I do this query efficiently?Rolando Edwards13 Nov
      • Re: How do I do this query efficiently?Sebastiaan van Erk13 Nov
    • Re: How do I do this query efficiently?Sebastiaan van Erk13 Nov
      • Re: How do I do this query efficiently?Peter Brawley13 Nov
        • Re: How do I do this query efficiently?Rolando Edwards13 Nov
        • Re: How do I do this query efficiently?Sebastiaan van Erk13 Nov
Re: How do I do this query efficiently?Peter Brawley16 Nov
  • Re: How do I do this query efficiently?Manuel Vacelet17 Nov