Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按周(7天)分组查询结果-MySql_Mysql - Fatal编程技术网

按周(7天)分组查询结果-MySql

按周(7天)分组查询结果-MySql,mysql,Mysql,我有一个查询,输出(排序)从2017年3月3日至今每天的日期和计数。有没有办法按周(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)

我有一个查询,输出(排序)从2017年3月3日至今每天的日期和计数。有没有办法按周(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)
结果(部分):

通缉结果:

'3485',  '03/03/17'
'7716',  '10/03/17'
编辑:使用每周分组(a.datecreated)的结果:

p.S: 如果一周(7天)可以从2017年3月5日(2017年3月5日)开始,这将是完美的,因为这是一个星期一,第3天和第4天将保留为单个数字

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)
使用此行
按周分组(STR_TO_DATE(a.datecreated,'%d/%m/%y'))

您正在将字符串字段转换为日期,然后将该日期转换为星期

您可以尝试上述查询。

应使用:

GROUP BY WEEK(a.datecreated, 7)

或者查看第二个参数的值。

已经尝试过此方法,但不起作用。结果(所有结果):
'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'
您没有尝试按周使用
分组(stru-TO-u-u-DATE创建日期(a.DATE s),%d/%m/%y)
只要尝试一次,你就会得到你想要的准确结果。请看:你的意思是什么?@草莓请帮助我,而不是仅仅链接东西当我使用一个词时,它的意思就是我选择它的意思-不多也不少。在本例中,在提供的链接上接受的答案说明了我想说的一切。@strybride You downvoter??
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)
GROUP BY WEEK(a.datecreated, 7)