在Mysql中按weekno和year排序

时间:2013-02-14 15:29:29

标签: mysql

select abc.mnth as mnth,sum(abc.info) as info from (select aaa.mnth as mnth, aaa.info as info from ((select ontime.mnth as mnth, ontime.info as info from (SELECT OnTimeRecords.mnth as mnth , round(((OnTimeRecords.count/completed.count)*100),2) as info
  from (SELECT DATE_FORMAT(t.dtDelivery,'%V %X') as mnth, count(*) as count FROM table1 t
     WHERE fCompleted = 1
     AND t.fStatus = 1
     AND t.dtDelivery >= '2013-01-01 00:00:00'
     AND t.dtDelivery <= '2013-12-28 23:59:59'
     AND FIND_IN_SET(t.sProjectCode,'All,pj1,pj2,hhfgh,tewrtert,dfgdgdf,Project Code,null,tyuire,dfgdfgdf,[select]') > 0
     AND t.dtDelivery  <= t.dtDeliveryDue
     group by DATE_FORMAT(t.dtDelivery,'%V %X')
     order by DATE_FORMAT(t.dtDelivery,'%V %X')) as completed
  Inner join (SELECT DATE_FORMAT(t.dtDelivery,'%V %X') as mnth, count(*) as count FROM table1 t
     WHERE fCompleted = 1
     AND t.fStatus = 1
     AND FIND_IN_SET(t.sProjectCode,'All,pj1,pj2,hhfgh,tewrtert,dfgdgdf,Project Code,null,tyuire,dfgdfgdf,[select]') > 0
     AND t.dtDelivery >= '2013-01-01 00:00:00'
     AND t.dtDelivery <= '2013-12-28 23:59:59'
     AND t.dtDelivery  <= t.dtDeliveryDue
     group by DATE_FORMAT(t.dtDelivery,'%V %X')
     order by DATE_FORMAT(t.dtDelivery,'%V %X')) as OnTimeRecords on OnTimeRecords.mnth = completed.mnth) as ontime)
union all
(select monthdates.mnth as mnth, monthdates.info as info from
    ((select  distinct(DATE_FORMAT(aDate,'%V %X')) as mnth ,0 as info  from (
    select @maxDate - interval (a.a+(10*b.a)+(100*c.a)+(1000*d.a)) day aDate from
    (select 0 as a union all select 1 union all select 2 union all select 3
     union all select 4 union all select 5 union all select 6 union all
     select 7 union all select 8 union all select 9) a, /*10 day range*/
    (select 0 as a union all select 1 union all select 2 union all select 3
     union all select 4 union all select 5 union all select 6 union all
     select 7 union all select 8 union all select 9) b, /*100 day range*/
    (select 0 as a union all select 1 union all select 2 union all select 3
     union all select 4 union all select 5 union all select 6 union all
     select 7 union all select 8 union all select 9) c, /*1000 day range*/
    (select 0 as a union all select 1 union all select 2 union all select 3
     union all select 4 union all select 5 union all select 6 union all
     select 7 union all select 8 union all select 9) d, /*10000 day range*/
    (select @minDate :='2013-01-01 00:00:00', @maxDate :='2013-12-28 23:59:59') e
  ) f
  where aDate between @minDate and @maxDate
  order by aDate asc
)) as monthdates) ) as aaa
order by aaa.mnth desc) as abc
group by abc.mnth
order by  STR_TO_DATE (abc.mnth,'%V %X');

我想根据开始日期和结束日期生成报告。我想要周报。我的查询结果如下所示..

week                     info
02 2013          0
06 2013         100
01 2013          0
05 2013          0
53 2012          0
04 2013          0
08 2013         100
03 2013          0
07 2013         100

我希望我的结果如下

week                    info
53 2012         0
01 2013         0
02 2013         0
03 2013         0
04 2013         0
05 2013         0
06 2013        100
07 2013        100
08 2013        100

1 个答案:

答案 0 :(得分:0)

这个查询令人难以置信,似乎每个伪表选择语句都有一个ORDER BY

我会尝试删除所有ORDER BY,检查结果,然后(假设结果令人满意)最后应用一个ORDER BY - 可能只是ORDER BY abc.mnth这似乎是一个约会,你想按日期顺序。

相关问题