Re: short-cutting if sum()>constant - Mailing list pgsql-sql

From A. Kretschmer
Subject Re: short-cutting if sum()>constant
Date
Msg-id 20091222141231.GE10308@a-kretschmer.de
Whole thread Raw
In response to short-cutting if sum()>constant  (Ivan Sergio Borgonovo <mail@webthatworks.it>)
Responses Re: short-cutting if sum()>constant  (msi77 <msi77@yandex.ru>)
List pgsql-sql
In response to 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?

I think no.

But you can create a new table with 2 columns: status (primary key) and
a column for the sum of qty. And you need a TRIGGER: update this table
for every insert, update and delete on your table t.

Now you can ask _this_ new table if the sum(qty) > your constant, this
should work very fast.


Just an idea...


Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431  2EB0 389D 1DC2 3172 0C99


pgsql-sql by date:

Previous
From: Adam Tauno Williams
Date:
Subject: Re: short-cutting if sum()>constant
Next
From: msi77
Date:
Subject: Re: short-cutting if sum()>constant