Generating Running Sum of Ratings in SQL

Posted by Koobz on Stack Overflow See other posts from Stack Overflow or by Koobz
Published on 2010-05-25T04:26:29Z Indexed on 2010/05/25 4:31 UTC
Read the original article Hit count: 247

Filed under:
|

I have a rating table. It boils down to:

rating_value  created
+2            april 3rd
-5            april 20th

So, every time someone gets rated, I track that rating event in the database.

I want to generate a rating history/time graph where the rating is the sum of all ratings up to that point in time on a graph.

I.E. A person's rating on April 5th might be select sum(rating_value) from ratings where created <= april 5th

The only problem with this approach is I have to run this day by day across the interval I'm interested in. Is there some trick to generating a running total using this sort of data?

Otherwise, I'm thinking the best approach is to create a denormalized "rating history" table alongside the individual ratings.

© Stack Overflow or respective owner

Related posts about postgresql

Related posts about sum