Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:24:50+00:00 2026-06-09T02:24:50+00:00

First, this was my first question concerning that issue : SQL request to group

  • 0

First, this was my first question concerning that issue : SQL request to group and sum numbers by their day of creation

Now, imagine I want to make it more complex, after previous operations and thanks to answers, I have results grouped by day, so each entries of the day are added. I have this :

1 | 200 | 2010-01-01
2 | 100 | 2010-01-01

Transformed into this :

1 | 300 | 2010-01-01

That’s already pretty good, but what if I want the amount field to increment each time ?

1 | 300 | 2010-01-01
2 | 200 | 2010-01-02

Will become :

1 | 300 | 2010-01-01
2 | 500 | 2010-01-02
x | (previous amount + this amount) | this date

The sql query I got thanks to answers to my previous question :

select sum(amount), to_char(date, 'YYYY-MM-DD') 
from mytable 
group by to_char(date, 'YYYY-MM-DD') 
order by sum(amount) ASC;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:24:51+00:00Added an answer on June 9, 2026 at 2:24 am

    A running sum can be done using windowing functions:

    select sum(amount) over (order by date asc), 
           to_char(date, 'YYYY-MM-DD') 
    from mytable 
    order by date ASC;
    

    If you want the running sum over the aggregated result, just put it into a sub-query:

    select sum(day_total) over (order by date) as running_total,
           day_total, 
           date
    from (
      select sum(amount) as day_total, 
             date
      from mytable 
      group by date
    ) t
    order by date
    

    If the column date is actual of the data type date, then there is no reason to use to_char() on it. Btw: it’s not a terribly good idea to use reserved words (date) for column names.

    The important thing here is the order by as part of the over definition.

    More details in the manual: http://www.postgresql.org/docs/current/static/tutorial-window.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The first part of this question is actually a request for confirmation based on
The first part of this question is now its own, here: Analyzing Text for
I'm trying to test the first answer to this question: SQL - message schema
First of all: This question is not directly programming related. However, the problem only
I'm using the first answer to this question Overcoming Python's limitations regarding instance methods
I've looked at this question: Select first instance only with XPath? But if I
This is my first time here so I hope I post this question at
This is my first Bash script so forgive me if this question is trivial.
This question arose from the discussion in the comments of this answer . First,
This question is a bit of a two-parter. First, the title question. Here's what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.