简体   繁体   中英

How to select past six months sales value?

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!!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM