Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 4664D9FBC92 for ; Thu, 15 Mar 2007 15:55:10 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 16060-01 for ; Thu, 15 Mar 2007 15:54:59 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from zebedee.cranntara.net (zebedee.cranntara.net [91.186.7.79]) by postgresql.org (Postfix) with ESMTP id 37A969FBBB3 for ; Thu, 15 Mar 2007 15:55:00 -0300 (ADT) Received: from dial-dynamic-62-69-37-165.surfdial.murphx.net ([62.69.37.165] helo=bolek.coachhouse) by zebedee.cranntara.net with esmtpsa (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.62) (envelope-from ) id 1HRv6F-0001Ym-OX; Thu, 15 Mar 2007 18:54:57 +0000 Received: from [192.168.0.32] (helo=[192.168.0.32]) by bolek.coachhouse with esmtp (Exim 4.50) id 1HRv66-0008LP-5i; Thu, 15 Mar 2007 18:54:46 +0000 Message-ID: <45F99675.5050801@numerixtechnology.de> Date: Thu, 15 Mar 2007 18:54:45 +0000 From: T E Schmitz Reply-To: mailreg@numerixtechnology.de User-Agent: Debian Thunderbird 1.0.7 (X11/20051017) X-Accept-Language: en-us, en MIME-Version: 1.0 To: =?ISO-8859-1?Q?William_Leite_Ara=FAjo?= CC: pgsql-sql@postgresql.org Subject: Re: join table with itself? References: <45F96A04.7020307@numerixtechnology.de> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/216 X-Sequence-Number: 27854 William Leite Araújo wrote: > On 15/03/07, *T E Schmitz* > wrote: > (...) > > Try join the tables. > > SELECT present.day, present.low, (MIN(future.day)-present.day) as > > days2fall FROM history AS present JOIN history AS future ON ( > present.day < future.day AND > future.low <= present.low ) > GROUP BY present.day,present.low > ORDER BY days2fall DESC That produces the same result as my previous example but maybe the join is more efficient, Thank you for the suggestion. However, I am still stuck as to how to retrieve HIGHEST. The result set produced by the above query only contains those tuples whose LOW is lower than present.LOW. For HIGHEST, I need to look at the rows between present.day and DAYS2FALL: something like SELECT MAX (high) from history WHERE day >= present.day AND day < (present.day + days2fall) - Regards, Tarlika Elisabeth Schmitz