计算一系列日期的每周数据

时间:2014-01-20 08:54:05

标签: mysql sql datetime select group-by

我可以从2012-12-012013-01-09说出日期范围。我想获取每周记录数量的数据,它应该是每周开始日期的7天。所以2012-12-012012-12-07week1等等......

表格如下,

+-ID-+--items---+---dated---+
+----+----------+-----------+
| 1  | 10       |2012-12-01 |
| 2  | 2        |2012-12-02 |
| .  | .        |   .       |
| .  | .        |   .       |
| .  | .        |   .       |
+----+----------+-----------+

初始查询Select count(items),week(date) from item_data

3 个答案:

答案 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