Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eE9gT-0005mB-IB for pgsql-sql@arkaria.postgresql.org; Mon, 13 Nov 2017 08:01:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1eE9gT-0000mF-4h for pgsql-sql@arkaria.postgresql.org; Mon, 13 Nov 2017 08:01:33 +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 1eE9gS-0000lx-HH for pgsql-sql@postgresql.org; Mon, 13 Nov 2017 08:01:32 +0000 Received: from host3.dynacom.ondsl.gr ([62.103.35.211] helo=smadev.internal.net) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eE9gP-0003JE-FA for pgsql-sql@postgresql.org; Mon, 13 Nov 2017 08:01:32 +0000 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id vAD81Ru2022984 for ; Mon, 13 Nov 2017 10:01:27 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) Subject: Re: md5 checksum of a previous row To: pgsql-sql@postgresql.org References: From: Achilleas Mantzios Message-ID: <2849b22d-ffff-9925-ebbc-7eeb1d778af5@matrix.gatewaynet.com> Date: Mon, 13 Nov 2017 10:01:27 +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: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 +1 , cool! On 13/11/2017 09:49, Thomas Kellerer wrote: >> 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 > > > > > > -- 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