Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dbMlJ-0002JI-DE for pgsql-sql@arkaria.postgresql.org; Sat, 29 Jul 2017 08:06:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dbMlH-0005tX-EO for pgsql-sql@arkaria.postgresql.org; Sat, 29 Jul 2017 08:06:11 +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 1dbMlE-0005qU-2P for pgsql-sql@postgresql.org; Sat, 29 Jul 2017 08:06:08 +0000 Received: from [195.159.176.226] (helo=blaine.gmane.org) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dbMlA-0002hg-De for pgsql-sql@postgresql.org; Sat, 29 Jul 2017 08:06:06 +0000 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1dbMl1-0006dS-At for pgsql-sql@postgresql.org; Sat, 29 Jul 2017 10:05:55 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Harald Fuchs Subject: Re: Issues with lag command Date: Sat, 29 Jul 2017 10:05:52 +0200 Lines: 43 Message-ID: <87r2wzacrj.fsf@protecting.net> References: Mime-Version: 1.0 Content-Type: text/plain X-Complaints-To: usenet@blaine.gmane.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-Archive: encrypt X-PGP-Fingerprint: 09 4E C4 A2 B2 C5 33 1A 79 80 5D 39 BD 9B 89 39 X-Face: (1awP+uzUZkz*UdAvr%F%K`x9g3n,CWkrK[r6TS,kY~DP)$C&=IJQ;H0uPn0B$Rb>\w"Tk~9w';1@dNIad{z!y(R99X7d-uc~Vf%,i.:=~=V>.b_)hr36Jt.tF0OBe]&PB6F.(k[i^^v^8DBny^)@17gud{[!1jfZ8+ Cancel-Lock: sha1:1ey/GCdaddPLOs1hWF3HjrJ/RhQ= X-Host-Lookup-Failed: Reverse DNS lookup failed for 195.159.176.226 (failed) 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 Igor Neyman writes: > Hello > I have a test table with the following structure (2 columns: ID and time_id )and data > > ID, time_id > 1;"2015-01-01" > 2;"" > 3;"" > 4;"2015-01-02" > 5;"" > 6;"" > 7;"" > 8;"2015-01-03" > 9;"" > 10;"" > 11;"" > 12;"" > 13;"2015-01-05" > 14;"" > 15;"" > 16;"" > I'd like to update line 2 and 3 with the date in record 1 (2015-01-01) > Update line 5,6 and 7 with the date in record 4 (2015-01-02) and so on > How about simple SQL instead of PlSql: > > UPDATE test T1 SET time_id = (SELECT T2.time_id FROM test T2 WHERE T2.id = > (SELECT max(T3.id) FROM test T3 WHERE T3.id < T1.id AND T3.time_id IS NOT NULL) > ) > WHERE T1.time_id IS NULL; You don't need that many table aliases: UPDATE test SET time_id = ( SELECT T1.time_id FROM test T1 WHERE T1.id < test.id AND T1.time_id IS NOT NULL ORDER BY T1.id DESC LIMIT 1 ) WHERE time_id IS NULL; -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql