Re: Searching for big differences between values

From: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
To: Durumdara <durumdara(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Searching for big differences between values
Date: 2017-11-30 11:36:22
Message-ID: 20171130113622.5vuts6vkz25dztwz@campbell-lange.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 30/11/17, Durumdara (durumdara(at)gmail(dot)com) wrote:
> Somewhere the users made mistakes on prices (stock).
>
> I need to search for big differences between values.
> For example:
>
> 20
> 21
> 21,5
> 30
> 28
> ..
> 46392 <-
> 46392 <-

You could use window functions
https://www.postgresql.org/docs/current/static/functions-window.html

Eg
costings=> create table vals (num integer);

costings=> insert into vals values (20), (21), (30), (28), (46392);

costings=> select num, num - lag(num, 1, num) over () as diff from vals
order by num;
num | diff
-------+-------
20 | 0
21 | 1
28 | -2
30 | 9
46392 | 46364
(5 rows)

Although you might want to use averaging or percentages to throw up
errors instead.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Mike Sofen 2017-11-30 13:02:46 RE: copy error with json/jsonb types
Previous Message Chris Mair 2017-11-30 11:00:41 Re: Searching for big differences between values