Lists: | sfpug |
---|
From: | Aditya <aditya(at)grot(dot)org> |
---|---|
To: | sfpug(at)postgresql(dot)org |
Subject: | running aggregates entirely in SQL? |
Date: | 2004-05-11 17:10:58 |
Message-ID: | 20040511171058.GC69933@mighty.grot.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | sfpug |
I could have sworn that I saw an email from Josh Berkus on sfpug about how to
do running aggregates (ie. a running tally or total, say in an invoice)
entirely in PostgreSQL, however googling only finds:
http://archives.postgresql.org/pgsql-novice/2003-09/msg00305.php
and I can't find it in the sfpug archives:
http://archives.postgresql.org/sfpug/
am I mistaken?
If so, is there a way to keep a running tally entirely in PostgreSQL?
Thanks,
Adi
From: | elein <elein(at)varlena(dot)com> |
---|---|
To: | Aditya <aditya(at)grot(dot)org> |
Cc: | sfpug(at)postgresql(dot)org |
Subject: | Re: running aggregates entirely in SQL? |
Date: | 2004-05-11 17:27:10 |
Message-ID: | 20040511102710.E7232@cookie.varlena.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | sfpug |
I have information on running aggregates.
Let me dig up my references. The examples
for plpython are on my website at
www.varlena.com/GeneralBits/Tidbits
under talks for last year's OSCON.
But I think I have another article or two on them.
--elein
============================================================
elein(at)varlena(dot)com Varlena, LLC www.varlena.com
PostgreSQL Consulting, Support & Training
PostgreSQL General Bits http://www.varlena.com/GeneralBits/
=============================================================
I have always depended on the [QA] of strangers.
On Tue, May 11, 2004 at 01:10:58PM -0400, Aditya wrote:
> I could have sworn that I saw an email from Josh Berkus on sfpug about how to
> do running aggregates (ie. a running tally or total, say in an invoice)
> entirely in PostgreSQL, however googling only finds:
>
> http://archives.postgresql.org/pgsql-novice/2003-09/msg00305.php
>
> and I can't find it in the sfpug archives:
>
> http://archives.postgresql.org/sfpug/
>
> am I mistaken?
>
> If so, is there a way to keep a running tally entirely in PostgreSQL?
>
> Thanks,
> Adi
From: | Brian Ghidinelli <brian(at)pukkasoft(dot)com> |
---|---|
To: | sfpug(at)postgresql(dot)org |
Subject: | Re: running aggregates entirely in SQL? |
Date: | 2004-05-11 18:01:13 |
Message-ID: | 40A114E9.9040502@pukkasoft.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | sfpug |
Joe Celko has some examples in his "SQL for Smarties" book for doing
running aggregates entirely in SQL. I believe they will work in most
database servers. I don't have the book handy but it's a place to look.
On the Celko note - has anyone implemented his nested tree model? It's
really great for arbitrarily deeply nested hierarchies.
Brian
> On Tue, May 11, 2004 at 01:10:58PM -0400, Aditya wrote:
>
>>I could have sworn that I saw an email from Josh Berkus on sfpug about how to
>>do running aggregates (ie. a running tally or total, say in an invoice)
>>entirely in PostgreSQL, however googling only finds:
>>
>> http://archives.postgresql.org/pgsql-novice/2003-09/msg00305.php
>>
>>and I can't find it in the sfpug archives:
>>
>> http://archives.postgresql.org/sfpug/
>>
>>am I mistaken?
>>
>>If so, is there a way to keep a running tally entirely in PostgreSQL?
From: | David Fetter <david(at)fetter(dot)org> |
---|---|
To: | SF Postgres <sfpug(at)postgresql(dot)org> |
Subject: | Re: running aggregates entirely in SQL? |
Date: | 2004-05-11 18:43:36 |
Message-ID: | 20040511184336.GB31365@fetter.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | sfpug |
On Tue, May 11, 2004 at 11:01:13AM -0700, Brian Ghidinelli wrote:
>
> Joe Celko has some examples in his "SQL for Smarties" book for doing
> running aggregates entirely in SQL. I believe they will work in most
> database servers. I don't have the book handy but it's a place to look.
It's handy :)
BTW, one way I've done it is to create a temporary sequence, then use
it to impose an ordered row structure.
> On the Celko note - has anyone implemented his nested tree model?
> It's really great for arbitrarily deeply nested hierarchies.
See contrib/ltree.
Cheers,
D
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778
Remember to vote!
From: | Josh Berkus <josh(at)agliodbs(dot)com> |
---|---|
To: | Brian Ghidinelli <brian(at)pukkasoft(dot)com>, sfpug(at)postgresql(dot)org |
Subject: | Re: running aggregates entirely in SQL? |
Date: | 2004-05-11 19:31:10 |
Message-ID: | 200405111231.10963.josh@agliodbs.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | sfpug |
Brian,
> On the Celko note - has anyone implemented his nested tree model? It's
> really great for arbitrarily deeply nested hierarchies.
I have. I find it really, really fast for "in branch" queries (which other
trees suck at) but really, really slow to update. So it's a good model for
a tree which will be modified, say, once every 10,000 reads.
--
-Josh Berkus
Aglio Database Solutions
San Francisco
From: | Josh Berkus <josh(at)agliodbs(dot)com> |
---|---|
To: | David Fetter <david(at)fetter(dot)org>, SF Postgres <sfpug(at)postgresql(dot)org> |
Subject: | Re: running aggregates entirely in SQL? |
Date: | 2004-05-11 19:35:30 |
Message-ID: | 200405111235.30227.josh@agliodbs.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | sfpug |
David,
> > On the Celko note - has anyone implemented his nested tree model?
> > It's really great for arbitrarily deeply nested hierarchies.
>
> See contrib/ltree.
ltree isn't a nested set ... it's a combination text chain and adjacency list.
--
-Josh Berkus
Aglio Database Solutions
San Francisco
From: | Aditya <aditya(at)grot(dot)org> |
---|---|
To: | sfpug(at)postgresql(dot)org |
Subject: | Re: running aggregates entirely in SQL? |
Date: | 2004-05-11 20:37:08 |
Message-ID: | 20040511203708.GD71819@mighty.grot.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | sfpug |
On Tue, May 11, 2004 at 11:01:13AM -0700, Brian Ghidinelli wrote:
> Joe Celko has some examples in his "SQL for Smarties" book for doing
> running aggregates entirely in SQL. I believe they will work in most
> database servers. I don't have the book handy but it's a place to look.
Thanks for all the suggestions, and of course the most obvious way (in
hindsight) eluded me:
a table mytable with:
id, tdate, description, cost
and you can produce a "invoice" like thing with a running total as:
select
t.id,
t.tdate,
t.description,
t.cost,
(select sum(cost) from mytable where tdate <= t.tdate)
from
mytable t
order by
2
;
needless to say, the subselect gets more expensive as your table gets larger
so using an "aggregates" table is probably worth doing for large tables.
Adi
From: | Brian Ghidinelli <brian(at)pukkasoft(dot)com> |
---|---|
To: | sfpug(at)postgresql(dot)org |
Subject: | Re: running aggregates entirely in SQL? |
Date: | 2004-05-11 20:39:49 |
Message-ID: | 40A13A15.5050106@pukkasoft.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | sfpug |
Josh Berkus wrote:
> I have. I find it really, really fast for "in branch" queries (which other
> trees suck at) but really, really slow to update. So it's a good model for
> a tree which will be modified, say, once every 10,000 reads.
Same here. I've used it in a Sybase/MSSQL environment with Transact-SQL
stored procedures but my postgres kung-fu is nowhere near adequate to
replicate that functionality. I have used it for a message board system
that allowed deep threading successfully. I think it would also be good
for large organizational charts or family tree type models but I don't
have any of those really in my current project (motorsport event
management software).
Cheers,
Brian