Cumulative count (running total) window fn - Mailing list pgsql-general

From Oliver Kohll - Mailing Lists
Subject Cumulative count (running total) window fn
Date
Msg-id 70051B0A-F2EB-4C14-A614-09092B53F013@gtwm.co.uk
Whole thread Raw
Responses Re: Cumulative count (running total) window fn  (Magnus Hagander <magnus@hagander.net>)
List pgsql-general
Hello,

Many thanks to andreas.kretschmer for this helpful reply about how to set up a window function to perform a running
total:
http://archives.postgresql.org/pgsql-general/2010-03/msg01122.php

It works perfectly with the simple test data but I've just got back to work, tried implementing it on my live data and
theresults are slightly different. My query is almost exactly the same - I've simplified by grouping by year only
ratherthan year and month: 

select extract(year from signup_date),
  count(email_address),
  sum(count(email_address)) over (rows unbounded preceding)
from email_list group by 1 order by 1;

 date_part | count | sum
-----------+-------+------
      2007 |   501 | 1374
      2008 |   491 |  491
      2009 |   382 |  873
      2010 |    66 | 1440
(4 rows)

What I'm looking for is
 date_part | count | sum
-----------+-------+------
      2007 |   501 | 501
      2008 |   491 |  992
      2009 |   382 |  1374
      2010 |    66 | 1440

It seems to be adding up the counts but not in the right order.

I've also tried an explicit ORDER BY inside the partition with no difference:

select extract(year from signup_date),
  count(email_address),
  sum(count(email_address)) over (partition by 1 order by 1 asc rows unbounded preceding)
from email_list group by 1 order by 1;

Does anyone have any other ideas?

Regards
Oliver Kohll

oliver@agilebase.co.uk / +44(0)7814 828608 / skype:okohll
www.agilebase.co.uk - software
www.gtwm.co.uk - company




pgsql-general by date:

Previous
From: Piotr Kublicki
Date:
Subject: Re: Start-up script for few clusters: just add water?
Next
From: Magnus Hagander
Date:
Subject: Re: Cumulative count (running total) window fn