I have a date range lets say from 2012-12-01
and 2013-01-09
. I want to fetch weekly data for number of records, it should be 7 days from the start date for each week.So 2012-12-01
to 2012-12-07
is week1
and so on...
The table is as below,
+-ID-+--items---+---dated---+
+----+----------+-----------+
| 1 | 10 |2012-12-01 |
| 2 | 2 |2012-12-02 |
| . | . | . |
| . | . | . |
| . | . | . |
+----+----------+-----------+
Initial query Select count(items),week(date) from item_data
Try this ...
SELECT `id`, `items`, `dated`
FROM `table`
GROUP BY WEEK(`dated`)
SELECT SUM(items) AS total_items, CONCAT(dated, ' - ', dated + INTERVAL 6 DAY) AS week
FROM tablename
GROUP BY WEEK(dated)
ORDER BY WEEK(dated);
Try this:
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
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.