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

Posted

技术标签:

【中文标题】在mysql中计算数据并按周分组【英文标题】:counting data and grouping by week in mysql 【发布时间】:2011-04-08 03:08:46 【问题描述】:

这是我的数据库表:

    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);

我有很多关于这个表的数据。如果我想显示如下结果,我该怎么做:

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

一个月有很多数据。我想将它们分开计算一周。如果有数据达到了大约一周,那么脚本将自动计算它们并在第 1 周、第 2 周、第 3 周等共享它们。我怎么做?或者你有什么想法吗?使用YEARweek() 命令怎么样?

【问题讨论】:

几个月可能超过四个星期,您认为一周的开始/结束时间是什么时候? 指定你的mysql版本,你想依靠mysql还是外部脚本或者任务调度器来实现这个? 看看mysql event scheduler,前提是你使用的是mysql 5+ @Sandy:我正在使用 mysql 5.0 我是这个问题的新手。 【参考方案1】:

你需要做这样的事情

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)
...

【讨论】:

“BETWEEN 1 AND 7”是指 7 天吗? 表示月份中的某天在 1 到 7(含)之间【参考方案2】:

请看以下内容:

http://www.artfulsoftware.com/infotree/queries.php?bw=1280#78

索引页在这里:

http://www.artfulsoftware.com/infotree/queries.php?bw=1280

透视查询示例:

SELECT 
  IFNULL(empId,'Totals') AS EmpId,
  sums.2005, sums.2006, sums.2007,
  sums.2005 + sums.2006 + sums.2007 AS Sums
FROM (
  SELECT
    EmpID,
    SUM(IF(Yr=2005,sales,0)) As '2005',
    SUM(IF(Yr=2006,sales,0)) As '2006',
    SUM(IF(Yr=2007,sales,0)) As '2007'
  FROM Sales
  GROUP BY EmpID WITH ROLLUP
) AS sums;
+--------+----------+----------+----------+-----------+
| EmpId  | 2005     | 2006     | 2007     | Sums      |
+--------+----------+----------+----------+-----------+
| 1      | 12000.00 | 18000.00 | 25000.00 |  55000.00 |
| 2      | 15000.00 |  6000.00 |     0.00 |  21000.00 |
| 3      |     0.00 | 20000.00 | 24000.00 |  44000.00 |
| Totals | 27000.00 | 44000.00 | 49000.00 | 120000.00 |
+--------+----------+----------+----------+-----------+ 

【讨论】:

它是一个很好的教程..但如果你不回答我的问题,我无法解决我的问题。【参考方案3】:
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

【讨论】:

【参考方案4】:

您可以使用 SQL Server 的 Pivot 功能。请参考以下示例。

CREATE TABLE MyData
(
    ItemDate DATETIME,
    ItemName VARCHAR(200),
    ItemQty  INT
)
GO

DECLARE @I INT 
SET @i = 1
WHILE @i < 1000
BEGIN
    INSERT INTO MyData VALUES(GETDATE()-@I, 'NAME ' + CONVERT(VARCHAR(5),@I % 5), RAND()*100)
    SET @I = @I + 1
END
GO

SELECT ItemName, [1] AS 'WEEK-1', [2] AS 'WEEK-2', [3] AS 'WEEK-3', [4] AS 'WEEK-4'
FROM (SELECT ItemName, DATEPART(wk, ItemDate) WeekNum, ItemQty 
        FROM MyData) AS SourceTable
PIVOT
(
    SUM(ItemQty)
    FOR WeekNum IN ([1], [2], [3], [4])
) AS PivotTable;

【讨论】:

我不了解 SQL-Server。此代码与 mysql 不同。

以上是关于在mysql中计算数据并按周分组的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 按过去 24 小时、上周和上个月对促销进行分组,并按周降序排序

Mysql按周,按月,按日,按小时分组统计数据

MySql按周,按月,按日分组统计数据

MySql按周,按月,按日分组统计数据

MySql按周,按月,按日分组统计数据

SQL 分组按周和月在同一时间 (Redshift)