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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:31:55+00:00 2026-06-09T14:31:55+00:00

I have a table structure and sample data like this: +—-+———–+——+——-+———+ | id |

  • 0

I have a table structure and sample data like this:

+----+-----------+------+-------+---------+
| id | item_code | year | month | sales   |
+----+-----------+------+-------+---------+
| 1  |   12341   | 2011 |   10  | 1000000 |
+----+-----------+------+-------+---------+
| 2  |   12342   | 2011 |   11  | 2000000 |
+----+-----------+------+-------+---------+
| 3  |   12343   | 2011 |   12  | 3000000 |
+----+-----------+------+-------+---------+
| 4  |   12344   | 2012 |   01  | 4000000 |
+----+-----------+------+-------+---------+
| 5  |   12345   | 2012 |   02  | 5000000 |
+----+-----------+------+-------+---------+
| 6  |   12346   | 2012 |   03  | 6000000 |
+----+-----------+------+-------+---------+
| 7  |   12347   | 2012 |   04  | 6000000 |
+----+-----------+------+-------+---------+

Now my question is, for example the current month is August of 2012 (04 – 2012), how do we select the past six months data which are:

+----+-----------+------+-------+---------+
| id | item_code | year | month | sales   |
+----+-----------+------+-------+---------+
| 1  |   12341   | 2011 |   10  | 1000000 |
+----+-----------+------+-------+---------+
| 2  |   12342   | 2011 |   11  | 2000000 |
+----+-----------+------+-------+---------+
| 3  |   12343   | 2011 |   12  | 3000000 |
+----+-----------+------+-------+---------+
| 4  |   12344   | 2012 |   01  | 4000000 |
+----+-----------+------+-------+---------+
| 5  |   12345   | 2012 |   02  | 5000000 |
+----+-----------+------+-------+---------+
| 6  |   12346   | 2012 |   03  | 6000000 |
+----+-----------+------+-------+---------+

…and get the average sale value? Any ideas? 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-06-09T14:31:56+00:00Added an answer on June 9, 2026 at 2:31 pm

    The best thing to do would be to reorganize your tables to contain proper dates. You can keep these columns in there for specific purposes if you need, but having proper dates will let you use powerful SQL functions based around EXACTLY what you need to do right now.

    You will be able to do something like this for example:

    select someData from yourTable where columnDate>=date_sub(now(), interval 6 month);
    

    As eggyal correctly mentions, you will be able to easily get a lot of aggregate functions like this:

    select avg(sales) from ...
    

    Which will bring back the average sales over the conditions (like last 6 months) that you put into the where clause.

    If you don’t even keep your current year/month columns you can get that info out of a date column with something like this:

    select date_format(columnDate, '%Y') from .... // output: 2012
    

    What’s that? You want the month now?

    select date_format(columnDate, '%m') from .... // output: 08
    

    What’s that? You want the full month name? BAM!

    select date_format(columnDate, '%M') from .... // output: August
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an table structure like this mysql> SELECT id, name, parent_id FROM categories;
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I have these two table with some data sample in them. I would like
I have following table structure, with many tables like this: data_1: +-------+--------+-------+ | views
I have a table Sample and another SampleLog With these structure I want to
I have a simple table maintaining messages between users. The table structure looks like
I have a simple table that has this structure: <table> <thead> <tr> <td>Some info</td>
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have a StaffLookup table which looks like this. UserSrn | UserName | ManagerSrn
Suppose I have this database table (some sample code below) that stores the relationship

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.