Re: short-cutting if sum()>constant

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: short-cutting if sum()>constant
Date: 2009-12-22 19:47:18
Message-ID: 162867790912221147r7e3041fej44332a05faf915d6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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 <mail(at)webthatworks(dot)it>:
> 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(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Ivan Sergio Borgonovo 2009-12-22 23:00:31 Re: short-cutting if sum()>constant
Previous Message msi77 2009-12-22 19:15:02 Re: short-cutting if sum()>constant