agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Harald Fuchs <hari.fuchs@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: Issues with lag command
Date: Sat, 29 Jul 2017 10:05:52 +0200
Message-ID: <87r2wzacrj.fsf@protecting.net> (raw)
References: <CA+oNSn9i4PCRQKvwpGM3wRkWfMMSW6Mrc-9KsUdhWQceQFjcpA@mail.gmail.com>
<DM5PR17MB1499C3C8BA6133702E961C3FDABF0@DM5PR17MB1499.namprd17.prod.outlook.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
Igor Neyman <ineyman@perceptron.com> 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
view thread (5+ messages)
Message-ID: <87r2wzacrj.fsf@protecting.net>
Permalink: ../87r2wzacrj.fsf@protecting.net/
Also on: postgresql.org/message-id/87r2wzacrj.fsf@protecting.net
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: hari.fuchs@gmail.com
Subject: Re: Issues with lag command
In-Reply-To: <87r2wzacrj.fsf@protecting.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox