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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:20:52+00:00 2026-05-17T00:20:52+00:00

MY question deals with month to month data changes..I want a simple (if possible)

  • 0

MY question deals with month to month data changes..I want a simple (if possible) SQL query that will show the person , and month change grouped by person/month or similar.. the important part is to show gains and losses.

For Example I have a table: (date simplified )

id | Date      | Person | Sales
---|-----------|--------| -----
1  | 2010-9-01 | Alice  |  5
2  | 2010-8-01 | Alice  |  2

3  | 2010-9-01 | Peter  |  2
4  | 2010-8-01 | Peter  |  3

5  | 2010-9-01 | Bob    |  5
6  | 2010-8-01 | Bob    |  7

From this table I’m looking to get a report like this.

Month |  Person | Sales_total | Gain-Loss_from_Previous_month |
------  -------- ------------- -------------------------------
2010-9   Alice       7             +3
2010-9   Peter       5             -1
2010-9   Bob         12            -2

Can this be done as a pure MySQL query? O

  • 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-17T00:20:53+00:00Added an answer on May 17, 2026 at 12:20 am

    You may want to try something like the following:

    SELECT     CONCAT(YEAR(s1.date), '-', MONTH(s1.date)) month, 
               s1.person, 
               s2.total_sales sales_total,
               s1.sales - s3.sales gain_loss
    FROM       sales s1
    JOIN       (
                  SELECT person, SUM(sales) total_sales FROM sales GROUP BY person
               ) s2 ON (s2.person = s1.person)
    LEFT JOIN  sales s3 ON 
               (s3.date = DATE_SUB(s1.date, INTERVAL 1 MONTH) AND s3.person = s1.person)
    WHERE      s1.date = STR_TO_DATE(CONCAT('01-', MONTH(NOW()), '-', YEAR(NOW())), '%d-%m-%Y')
    GROUP BY   s1.person, s1.date
    ORDER BY   s1.person, s1.date;
    

    The WHERE clause part where s1.date = STR_TO_DATE(...) is basically restricting the s1.date to the current month and year. I have a feeling that there’s a neater solution, but at the moment I can’t think of a suitable alternative.

    Test case:

    CREATE TABLE sales (id int, date date, person varchar(40), sales int);
    
    INSERT INTO SALES VALUES (1, '2010-9-01', 'Alice', 5);
    INSERT INTO SALES VALUES (2, '2010-8-01', 'Alice', 2);
    INSERT INTO SALES VALUES (3, '2010-9-01', 'Peter', 2);
    INSERT INTO SALES VALUES (4, '2010-8-01', 'Peter', 3);
    INSERT INTO SALES VALUES (5, '2010-9-01', 'Bob', 5);
    INSERT INTO SALES VALUES (6, '2010-8-01', 'Bob', 7);
    

    Result:

    +--------+--------+-------------+-----------+
    | month  | person | sales_total | gain_loss |
    +--------+--------+-------------+-----------+
    | 2010-9 | Alice  |           7 |         3 |
    | 2010-9 | Bob    |          12 |        -2 |
    | 2010-9 | Peter  |           5 |        -1 |
    +--------+--------+-------------+-----------+
    3 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my question today deals with Flash AS3 video buffering. (Streaming or Progressive) I want
My question deals specifically with the mapping of numeric types between SQL Server 2008
I know this SO question, but it deals with the subject in more general
Really simple question. I forgot how to deal with this specific script. With a
I had a hard time framing that question. I hope it gets the gist
This is may not be a true programming question but deals with geolocation Api,
The other answers to this question deal with timing whereas with this problem deals
My question is similar to sql statement to delete records older than XXX as
This question deals with optional arguments passed to a Ruby block. I'm wondering if
Is it possible to change the power-saving behaviour of a laptop computer on lid

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.