Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eE9Ut-0004uH-Ag for pgsql-sql@arkaria.postgresql.org; Mon, 13 Nov 2017 07:49:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1eE9Us-0002Ef-KF for pgsql-sql@arkaria.postgresql.org; Mon, 13 Nov 2017 07:49:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eE9Uq-0002BV-GL for pgsql-sql@postgresql.org; Mon, 13 Nov 2017 07:49:32 +0000 Received: from [195.159.176.226] (helo=blaine.gmane.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eE9Uk-00031g-3l for pgsql-sql@postgresql.org; Mon, 13 Nov 2017 07:49:32 +0000 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1eE9Ua-0007Sj-Mt for pgsql-sql@postgresql.org; Mon, 13 Nov 2017 08:49:16 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Re: md5 checksum of a previous row Date: Mon, 13 Nov 2017 08:49:16 +0100 Lines: 29 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@blaine.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 In-Reply-To: Content-Language: de-DE 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 > But how do I use lag function or something like lag to read the previous record as whole. You can reference the whole row by using the table name: select created_at, value, value - lag(value, 1, 0.0) over(order by created_at) as delta, md5(lag(test::text) over(order by created_at)) as the_row FROM test ORDER BY created_at; The table reference "test" returns the whole row, e.g. something like: (bbf35815-479b-4b1b-83c5-5e248aa0a17f,52.00,,"2017-11-13 08:45:16.17231",B) that can be cast to text and then you can apply the md5() function on the result. It does include the parentheses and the commas, but as it does that for every row in a consistent manner, it shouldn't matter. > 2. insert the computed checksum in the current row That can also be done using the above technique, something like: new.hash := md5(new::text); Thomas -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql