我有一个表结构和示例数据,如下所示:
+----+-----------+------+-------+---------+
| 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 |
+----+-----------+------+-------+---------+
现在我的问题是,例如当前月份是 2012 年 8 月(04 - 2012),我们如何选择过去六个月的数据:
+----+-----------+------+-------+---------+
| 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 |
+----+-----------+------+-------+---------+
...并获得平均销售价值?有任何想法吗?蒂亚!!!