Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e9o7k-00089q-B7 for pgsql-sql@arkaria.postgresql.org; Wed, 01 Nov 2017 08:11:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e9o7j-0000S8-Nb for pgsql-sql@arkaria.postgresql.org; Wed, 01 Nov 2017 08:11:43 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e9o7j-0000Ry-5Q for pgsql-sql@postgresql.org; Wed, 01 Nov 2017 08:11:43 +0000 Received: from host3.dynacom.ondsl.gr ([62.103.35.211] helo=smadev.internal.net) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e9o7f-0003wv-SI for pgsql-sql@postgresql.org; Wed, 01 Nov 2017 08:11:42 +0000 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id vA18BaHM028749 for ; Wed, 1 Nov 2017 10:11:36 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) Subject: Re: join tables by nearest timestamp To: pgsql-sql@postgresql.org References: <282b209c-eaac-2f22-ae04-6ecfdac5284b@matrix.gatewaynet.com> From: Achilleas Mantzios Message-ID: <53afa96e-2c45-bb10-51f3-2a0640c5d743@matrix.gatewaynet.com> Date: Wed, 1 Nov 2017 10:11:36 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <282b209c-eaac-2f22-ae04-6ecfdac5284b@matrix.gatewaynet.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 01/11/2017 10:06, Achilleas Mantzios wrote: > On 01/11/2017 07:53, Brice André wrote: >> Dear all, >> >> I am running a postgresql 9.1 server and I have a table containing events information with, for each entry, an event type, a timestamp, and additional information. >> >> I would want to write a query that would return all events of type 'a', but each returned entry should be associated to the neraest event of type 'b' (ideally, the nearest, non taking into account >> if it happened before or after, but if not possible, it could be the first happening just after). >> >> By searching on the web, I found a solution base on a "LEFT JOIN LATERAL", but this is not supported by postgresql 9.1 (and I cannot update my server) : >> >> SELECT * >> FROM >> (SELECT * FROM events WHERE type = 'a' ) as t1 >> LEFT JOIN LATERAL >> (SELECT * FROM events WHERE type = 'b' AND timestamp >= t1.timestamp ORDER BY timestamp LIMIT  1) as t2 >> ON TRUE; >> >> Any idea on how to adapt this query so that it runs on 9.1 ? Or any other idea on how to perform my query ? > smth like : > > SELECT l1.*,l2.logtime,l2.category,l2.username from logging l1 LEFT OUTER JOIN  logging l2 ON ('t') where l1.category='vsl.login' AND (l2.category IS NULL OR l2.category='vsl.SpareCases') AND > (l2.logtime IS NULL OR l2.logtime>=l1.logtime) order by l1.logtime; oopss, sorry I forgot, you'll have to add a DISTINCT ON and order by l2.logtime in order to have what you want : SELECT DISTINCT ON (l1.logtime,l1.category,l1.username,l1.action) l1.*,l2.logtime,l2.category,l2.username from logging l1 LEFT OUTER JOIN  logging l2 ON ('t') where l1.category='vsl.login' AND (l2.category IS NULL OR l2.category='vsl.SpareCases') AND (l2.logtime IS NULL OR l2.logtime>=l1.logtime) order by l1.logtime,l1.category,l1.username,l1.action,l2.logtime; > >> >> Thanks in advance, >> Brice > > -- Achilleas Mantzios IT DEV Lead IT DEPT Dynacom Tankers Mgmt -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql