Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 27C5A632BFA for ; Tue, 22 Dec 2009 15:47:31 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 12802-03 for ; Tue, 22 Dec 2009 19:47:17 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-yw0-f179.google.com (mail-yw0-f179.google.com [209.85.211.179]) by mail.postgresql.org (Postfix) with ESMTP id 4966B63289B for ; Tue, 22 Dec 2009 15:47:20 -0400 (AST) Received: by ywh9 with SMTP id 9so385593ywh.19 for ; Tue, 22 Dec 2009 11:47:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=3zVpppI10ip55EGFXz7ULcfRJWH9L8LQ59R4XRRSD8g=; b=nEduOsbGC94UInYqQToJNTZehb5LBGEHSrfbrNuGSyQ7A/CBIp/p587VOJE0NVUusK 3mhOij+ylR6dYHvA6sKu2OuHS6RyCV1Xt9ekyydfzQH7nrZnQvu/xlXVioxbod8asi4t o/nCkvkGRfcMAVEg/v4eOusLcDbCAda2vxHho= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=M2/6Mwb/MPLEISaCX5zqpv+tEZeHRqaolnYkHOiQphlgrbfjucbG3HPIkM9VMDYZkS cf3d7O6yoQDIjuigCZkfzPp0zNi0Icg8CU00oiuu6114utlKW7qSY2NWzJKo0m3/D1Vg 1GE/JHu5hquM1ax3AfKMHplukzkqbfaUr2st8= MIME-Version: 1.0 Received: by 10.150.117.25 with SMTP id p25mr14214730ybc.139.1261511239018; Tue, 22 Dec 2009 11:47:19 -0800 (PST) In-Reply-To: <20091222133527.3007bd96@dawn.webthatworks.it> References: <20091222133527.3007bd96@dawn.webthatworks.it> Date: Tue, 22 Dec 2009 20:47:18 +0100 Message-ID: <162867790912221147r7e3041fej44332a05faf915d6@mail.gmail.com> Subject: Re: short-cutting if sum()>constant From: Pavel Stehule To: Ivan Sergio Borgonovo Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=UTF-8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.579 tagged_above=-10 required=5 tests=AWL=0.020, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/64 X-Sequence-Number: 33502 Hello I found one ugly trick. You can multiply lines and SUM > cons could be replaced limit clause: postgres=# select * from data; a --- 3 2 1 4 2 3 (6 rows) Then SELECT * FROM WHERE and stop when SUM(a) = n then postgres=# select generate_series(1,a) from data; generate_series ----------------- 1 2 3 1 2 1 1 2 3 4 1 2 1 2 3 So If I would to check if there are sum(a) >= 10 then I can use LIMIT 10. If query returns ten rows, then result is true, else result is false select a, (a = generate_series(1,a))::int from data limit 12; -- stop after sum(a) = 12 postgres=# select sum(x) from (select 1 as x,(a = generate_series(1,a))::int from data limit 12) s; sum ----- 12 -- 12 is eq 12, so test is successful (1 row) Regards Pavel Stehule 2009/12/22 Ivan Sergio Borgonovo : > Hi, > > I'd like to know if > > select sum(qty) from t where status=37; > > is > constant. > > qty is always >0. > > Is there a way to skip examining further rows and return a result > ASAP? > > > -- > Ivan Sergio Borgonovo > http://www.webthatworks.it > > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql >