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 4343466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:45:25+00:00 2026-05-21T11:45:25+00:00

I read Create a Cumulative Sum Column in MySQL , and tried to adapt

  • 0

I read Create a Cumulative Sum Column in MySQL, and tried to adapt it to what I’m doing, but I can’t seem to get it right.

The table:

Id (primary key)
AcctId
CommodId
Date
PnL

There is a unique index which contains AcctId, CommodId, Date. I want to get a cumulative total grouped by date.

This query

select c.date
   , c.pnl
   ,(@cum := @cum + c.pnl) as "cum_pnl"
   from commoddata c join (select @cum := 0) r
   where
   c.acctid = 2
   and
   c.date >= "2011-01-01"
   and
   c.date <= "2011-01-31"
   order by c.date

will correctly calculate the running total for all records, showing data in the format

date        pnl       cum_pnl
========    ======    =======
2011-01-01       1          1
2011-01-01       1          2
2011-01-01       1          3
2011-01-01       1          4
2011-01-02       1          5
2011-01-02       1          6
...

(there can be many records per date). What I want is

date        cum_pnl
========    =======
2011-01-01       4
2011-01-02       6
...

But nothing I’ve tried works. TIA.

  • 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-05-21T11:45:25+00:00Added an answer on May 21, 2026 at 11:45 am

    Alternately I think you can replace all your pnl with sum(pnl), and let your @cum run across those. I think it would look like this:

    select c.date
       ,SUM(c.pnl)
       ,(@cum := @cum + SUM(c.pnl)) as "cum_pnl"
       from commoddata c join (select @cum := 0) r
       where
       c.acctid = 2 and c.date >= "2011-01-01" and c.date <= "2011-01-31"
       order by c.date
       GROUP BY c.date
    

    I’m just trying to figure out if SQL will give you grief over selecting cum_pnl when it is not a group by expression… maybe you can try grouping by it as well?

    EDIT New Idea, if you’re really not averse to nested queries, replace commoddata with a summed grouped query

    select c.date
       ,c.pnl
       ,(@cum := @cum + c.pnl) as "cum_pnl"
       from 
           (SELECT date, sum(pnl) as pnl FROM commoddata WHERE [conditions] GROUP BY date) c 
           join (select @cum := 0) r
       order by c.date
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on a system where you can create-read-update-delete to/from a table in
how can i create an application to read all my browser (firefox) history? i
I'm trying to create a TCP connection and send/read data that uses SSL, but
I'm trying to create a Zip file from .Net that can be read from
Is it possible to create read only files in python which can not be
User can have the following access rights: Read = 1 Create = 2 Edit
Is there anything around that can read an XSD schema and dynamically create a
How can do i create ndef messages in byte form? I've read somewhere it
I want to create two users on my MySQL test database, One with read-only
What is a good way to create and read an OpenOffice spreadsheet in Perl?

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.