Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
844 views
in Technique[技术] by (71.8m points)

php - counting data and grouping by week in 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You would need to do something like this

SELECT
  n.name,
  w1.amount,
  w2.amount,
  ...
  IFNULL(w1.amount,0) + IFNULL(w2.amount,0) + .... AS total
FROM
   yourTable AS n
LEFT JOIN (
  SELECT name, SUM(qty) AS amount FROM yourTable WHERE DAY(date) BETWEEN 1 AND 7 GROUP BY name
) AS w1 USING (name)
LEFT JOIN (
  SELECT name, SUM(qty) AS amount FROM yourTable WHERE DAY(date) BETWEEN 8 AND 14 GROUP BY name
) AS w2 USING (name)
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...