我可以从2012-12-01
和2013-01-09
说出日期范围。我想获取每周记录数量的数据,它应该是每周开始日期的7天。所以2012-12-01
到2012-12-07
是week1
等等......
表格如下,
+-ID-+--items---+---dated---+
+----+----------+-----------+
| 1 | 10 |2012-12-01 |
| 2 | 2 |2012-12-02 |
| . | . | . |
| . | . | . |
| . | . | . |
+----+----------+-----------+
初始查询Select count(items),week(date) from item_data
答案 0 :(得分:1)
试试这个......
SELECT `id`, `items`, `dated`
FROM `table`
GROUP BY WEEK(`dated`)
答案 1 :(得分:0)
SELECT SUM(items) AS total_items, CONCAT(dated, ' - ', dated + INTERVAL 6 DAY) AS week
FROM tablename
GROUP BY WEEK(dated)
ORDER BY WEEK(dated);
答案 2 :(得分:0)
试试这个:
SELECT CEILING(DATEDIFF(dated, '2012-12-01')) AS weekNo, SUM(items) AS NoOfItems
FROM table
WHERE dated BETWEEN '2012-12-01' AND '2013-01-09'
GROUP BY weekNo