我有这个查询,从03-03-2017到现在输出(排序)每天的日期和每天的计数。有没有办法按周分组(7天)?
query.sql的:
SELECT SUM(a.ctr)
,a.datecreated
FROM
(
SELECT COUNT(*) as ctr, date_format(created, '%d/%m/%y ') as datecreated
FROM mimesi_indexer.meta_served_clips
GROUP BY date(datecreated)
UNION ALL
select 0 as ctr, date_format(selected_date, '%d/%m/%y ') as datecreated
from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date
between (SELECT MIN(created) FROM mimesi_indexer.meta_served_clips)
and (SELECT MAX(created) FROM mimesi_indexer.meta_served_clips)
) a
group by date(a.datecreated)
order by month(a.datecreated), date(a.datecreated)
结果(部分内容):
'279', '03/03/17'
'40', '04/03/17'
'41', '05/03/17'
'1223', '06/03/17'
'493', '07/03/17'
'580', '08/03/17'
'829', '09/03/17'
'936', '10/03/17'
'638', '11/03/17'
'345', '12/03/17'
'876', '13/03/17'
'1583', '14/03/17'
'1566', '15/03/17'
'1772', '16/03/17'
想要的结果:
'3485', '03/03/17'
'7716', '10/03/17'
编辑 结果按周使用分组(a.datecreated):
'38657', '03/03/17 '
'4773', '07/03/17 '
'28529', '01/04/17 '
'14191', '05/04/17 '
'34223', '01/05/17 '
'43093', '04/05/17 '
'82515', '01/06/17 '
'1804', '12/06/17 '
'23513', '01/07/17 '
'9287', '05/07/17 '
P.S: 如果一周(7天)可以从2017年3月5日(05-03-2017)开始,那将是完美的,因为这是星期一,第3和第4将保留为单个数字
答案 0 :(得分:0)
Activity
使用此行SELECT SUM(a.ctr)
,a.datecreated
FROM
(
SELECT COUNT(*) as ctr, date_format(created, '%d/%m/%y ') as datecreated
FROM mimesi_indexer.meta_served_clips
GROUP BY date(datecreated)
UNION ALL
select 0 as ctr, date_format(selected_date, '%d/%m/%y ') as datecreated
from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date
between (SELECT MIN(created) FROM mimesi_indexer.meta_served_clips)
and (SELECT MAX(created) FROM mimesi_indexer.meta_served_clips)
) a
group by week(STR_TO_DATE(a.datecreated,'%d/%m/%y'))
order by month(a.datecreated), date(a.datecreated)
。
您正在将字符串字段转换为日期,然后将该日期转换为星期。
您可以尝试以上查询。
答案 1 :(得分:0)