MySQL cumulative sum grouped by date

前端 未结 2 894
我寻月下人不归
我寻月下人不归 2020-12-01 10:56

I know there have been a few posts related to this, but my case is a little bit different and I wanted to get some help on this.

I need to pull some data out of the

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 11:56

    I figured out what I needed to do last night... but since I'm new to this I couldn't post it then... what I did that worked was this:

    SELECT
       DATE(e.Date) AS e_date,
       count(e.ID) AS num_daily_interactions,
       (
          SELECT 
             COUNT(id)
          FROM example 
          WHERE DATE(Date) <= e_date
       ) as total_interactions_per_day
    FROM example AS e
    GROUP BY e_date;
    

    Would that be less efficient than your query? I may just do the calculation in python after pulling out the count per day if its more efficient, because this will be on the scale of thousands to hundred of thousands of rows returned.

提交回复
热议问题