Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eKRjD-0008Ut-RC for pgsql-general@arkaria.postgresql.org; Thu, 30 Nov 2017 16:30:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eKRjD-0003NE-Dm for pgsql-general@arkaria.postgresql.org; Thu, 30 Nov 2017 16:30:23 +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 1eKRjD-0003N3-2x for pgsql-general@lists.postgresql.org; Thu, 30 Nov 2017 16:30:23 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eKRjA-0005Bq-6H for pgsql-general@postgresql.org; Thu, 30 Nov 2017 16:30:21 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 3BDCC4D2D4; Thu, 30 Nov 2017 17:30:16 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter03.heinlein-hosting.de (spamfilter03.heinlein-hosting.de [80.241.56.117]) (amavisd-new, port 10030) with ESMTP id cfObcqoUrEri; Thu, 30 Nov 2017 17:30:05 +0100 (CET) Message-ID: <1512059403.2328.14.camel@cybertec.at> Subject: Re: Searching for big differences between values From: Laurenz Albe To: Durumdara , pgsql-general@postgresql.org Date: Thu, 30 Nov 2017 17:30:03 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Durumdara 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 <- > > But it could be: > > 42300 > 43100 > 44000 > 43800 > 65000 <- > 42100 > > Human eye could locate these values, but there we need to check 30.000 articles and 450.000 values. > > Do you have any idea, how to this with SQL? You could use some variant of SELECT id, price FROM (SELECT id, price, avg(price) OVER (ORDER BY id ROWS BETWEEN 3 PRECEDING AND 3 FOLLOWING) AS av FROM stock) q WHERE NOT (price BETWEEN 0.8 * av AND 1.25 * av); Yours, Laurenz Albe