douyou7797 2010-09-04 03:07
浏览 78
已采纳

在mysql中按周计算数据和分组

this is my DB table:

    CREATE TABLE IF NOT EXISTS `inspection_report` (
    `Inspection_datetime` datetime NOT NULL,
    `Line` char(5) NOT NULL,
    `S` int(11) NOT NULL,
    `A` int(11) NOT NULL,
    `B` int(11) NOT NULL,
    `C` int(11) NOT NULL,
INSERT INTO `inspection_report` (`Inspection_datetime`,`Line`,`S`, `A`, `B`, `C`) VALUES
('2010-09-01 09:08:01','FA 05',0, 0, 0, 0),('2010-09-02 14:24:35','FA 07',0, 0, 1, 0),('2010-09-01 09:08:01','fa 05',0, 1, 1, 0),('2010-09-01 16:24:04','FA 03', 0, 1, 0, 0);

I have a lot of data for this table.how do i do if i want show the result like:

Line      1st week        2nd week      3rd week   4th week    5th week   total
   FA 03        20                32          10         12          35        109
   FA 05        12                 5          10         10          25         62
   FA 07         0                 0           1         1            0          2

there are a lot of data for a month. i want separate them counting for a week.if there is data that has reached about a week, then the script will automatically count them and share them in the 1st week,2nd week,3rd week,and so on. how do i do that? or are you have any idea? How about using YEARweek() command?

  • 写回答

4条回答 默认 最新

  • duanmei9980 2010-09-08 03:21
    关注
    SELECT
      A.Line,
      week1.1stweek,
      week2.2ndweek,
      ...
      IFNULL(week1.1stweek,0) + IFNULL(week2.2ndweek,0) + .... AS total
    FROM
       inspection_report AS A
    LEFT JOIN (
      SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS 1stweek FROM inspection_report WHERE DAY(Inspection_datetime) BETWEEN 1 AND 7 GROUP BY Line, WEEK, YEAR) AS week1 USING (Line)
    LEFT JOIN (
      SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS 2ndweek FROM inspection_report WHERE DAY(Inspection_datetime) BETWEEN 8 AND 14 GROUP BY Line, WEEK, YEAR) AS week2 USING (Line)
    ...
    GROUP BY Line
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集