我试图从MySQL表中获取行数,其中数据按WEEK分组。
到目前为止,我的查询是:
"SELECT count(*) as tweets, twitTimeExtracted as date
FROM scene.twitData
group by week(twitTimeExtracted)"
此查询返回以下数据:
如您所见,周数不正确,我期待从1月7日(7,14,21,28,4,11等等)开始每周的数据并持续到本周
我还尝试了orignal查询的修改版本:
SELECT count(*) as tweets, twitTimeExtracted as date
FROM scene.twitData
WHERE date(twitTimeExtracted)
BETWEEN '2013-01-07' and '2013-03-11'
group by week(twitTimeExtracted)
返回与第一个查询类似的结果。
可能存在与DATETIME中存储的一些数据不一致:twitTimeExtracted列中的几行数据?我真的不知道我对MySQL不是很有经验。
真的很感激任何帮助。
由于
答案 0 :(得分:17)
这会将datetime
值转换为适当的星期一
select count(*) as tweets,
str_to_date(concat(yearweek(twitTimeExtracted), ' monday'), '%X%V %W') as `date`
from twitData
group by yearweek(twitTimeExtracted)
yearweek
返回周和年。与字符串monday
一起,str_to_date
为您提供星期一的datetime
值。
如果您的一周从Monday开始,请改为使用yearweek(twitTimeExtracted, 1)
。
答案 1 :(得分:0)
始终获得星期一的一个选择是使用adddate:
SELECT count(*) as tweets, adddate(twitTimeExtracted, INTERVAL 2-DAYOFWEEK(twitTimeExtracted) DAY)
FROM twitData
WHERE date(twitTimeExtracted)
BETWEEN '2013-01-07' and '2013-03-11'
GROUP BY adddate(twitTimeExtracted, INTERVAL 2-DAYOFWEEK(twitTimeExtracted) DAY)
答案 2 :(得分:0)
试试这个:
SELECT count(*) as tweets,
date_add('7 Jan 2012', day, datediff('7 Jan 2012', twitTimeExtracted)) % 7
FROM scene.twitData
group by date_add('7 Jan 2012', day, datediff('7 Jan 2012', twitTimeExtracted)) % 7
答案 3 :(得分:0)
您也可以尝试这个。
select
DATE_ADD(DATE(date), INTERVAL (7 - DAYOFWEEK(date)) DAY) as weekend,
count(tweets)
from scene.twitData
group by weekend;