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

  • Home
  • SEARCH
  • 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 8146615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:03:54+00:00 2026-06-06T14:03:54+00:00

First time post. As an example, say I have a table with 12 entries;

  • 0

First time post. As an example, say I have a table with 12 entries;

+--------+------------+
| name   | date       |
+--------+------------+
| P1     | 2011-12-01 |
| P2     | 2012-01-01 |
| P3     | 2012-02-01 |
| P4     | 2012-02-01 |
| P5     | 2012-02-01 |
| P6     | 2012-05-01 |
| P7     | 2012-04-01 |
| P8     | 2012-05-01 |
| P9     | 2012-05-01 |
| P10    | 2012-06-01 |
| P11    | 2012-06-01 |
| P12    | 2012-03-01 |
+--------+------------+

I have grouped the information within the table by the date, kept a count of all of the id’s (not shown) and created a running total. Which I have implemented as so;

(SELECT date, c as count, (@s := @s + c) as run_total 
FROM
(SELECT D1.date, Count(D1.person_id) as c, @s := 0
FROM table D1
GROUP BY month(D1.date)
ORDER BY D1.date) as T1);

Output;

+------------+-------+-----------+
| date       | count | run_total |
+------------+-------+-----------+
| 2011-12-20 |     1 |         1 |
| 2012-01-31 |     1 |         2 |
| 2012-02-20 |     3 |         5 |
| 2012-03-05 |     1 |         6 |
| 2012-04-20 |     1 |         7 |
| 2012-05-20 |     3 |        10 |
| 2012-06-01 |     2 |        12 |
+------------+-------+-----------+

Good so far. This is where I loose it. How can I extract the most recent 4 entries and get something that looks like this??? where the dates remain in ascending order and more importantly the run_total stays the same;

—–What I am trying to achieve—–

----------------------------------
| 2012-03-05 |     1 |         6 |
| 2012-04-20 |     1 |         7 |
| 2012-05-20 |     3 |        10 |
| 2012-06-01 |     2 |        12 |
+------------+-------+-----------+

————————————-

In trying to work this out I created this;

(SELECT date, c as count, (@s := @s + c) as run_total
FROM
(SELECT D1.date, Count(D1.person_id) as c, @s := 0
FROM table D1
GROUP BY month(D1.date)
ORDER BY D1.date desc limit 4) as T1)
ORDER BY date;

But I did not get the desired outcome;

+------------+-------+-----------+
| date       | count | run_total |
+------------+-------+-----------+
| 2012-03-05 |     1 |         1 |
| 2012-04-20 |     1 |         2 |
| 2012-05-20 |     3 |         5 |
| 2012-06-01 |     2 |         7 |
+------------+-------+-----------+

The dates are in order but the run_totals are all messed up. 1 is supposed to be 6, 2 -> 7, 5 -> 10, and 7 -> 12 as shown earlier.

Again, what I am trying to achieve is;

----------------------------------
| 2012-03-05 |     1 |         6 |
| 2012-04-20 |     1 |         7 |
| 2012-05-20 |     3 |        10 |
| 2012-06-01 |     2 |        12 |
+------------+-------+-----------+
  • 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-06T14:03:55+00:00Added an answer on June 6, 2026 at 2:03 pm
    SELECT * FROM
    (SELECT *
    FROM (SELECT date, count, (@s := @s + c) as run_total 
    FROM
    (SELECT D1.date, Count(D1.person_id) as c, @s := 0
    FROM table D1
    GROUP BY month(D1.date)
    ORDER BY D1.date) as T1) tmp
    ORDER BY `date` DESC
    LIMIT 4) tmp2
    ORDER BY `date` ASC
    

    It takes the 4 most recent records of your query, then reorders them by date.
    The totals remain unchanged.

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

Sidebar

Related Questions

First time dealing with json. I have a php file that processes the post
This is my first time here so I hope I post this question at
Every time I try to post to twitter in my rails app, the first
this is my first post on this website, but I'm all the time getting
first time use JTree. Just wondering is it possible to have more than one
First time Stack Overflow poster. Please bear with me! :) I have a set
Ok, long time visitor, first time post. Instead of PowerShell telling me the result
I'm using ajax for the first time and have hit a bit of a
First-time post and python newb who has exhausted all other options. I am interested
this is my first post. I have a huge problem which make me headaches.

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.