计算过去四周的每周平均值

时间:2018-04-02 06:15:28

标签: mysql sql

我在MySQL DB(版本5.5.31)中有一个表格,表格为table_1,其中包含描述:

mysql> desc table_1;
+-------+-------------+------+-----+-------------------+-------+
| Field | Type        | Null | Key | Default           | Extra |
+-------+-------------+------+-----+-------------------+-------+
| col_1 | varchar(50) | NO   | PRI | NULL              |       |
| col_2 | varchar(50) | NO   | PRI | NULL              |       |
| col_3 | date        | NO   | PRI | NULL              |       |
| col_4 | int(11)     | NO   |     | NULL              |       |
| col_5 | int(11)     | NO   |     | NULL              |       |
| col_6 | float       | NO   |     | NULL              |       |
| col_7 | timestamp   | NO   |     | CURRENT_TIMESTAMP |       |
+-------+-------------+------+-----+-------------------+-------+
7 rows in set (0.01 sec)

DDL:

CREATE TABLE IF NOT EXISTS table_1
(
col_1 VARCHAR(50) NOT NULL,
col_2 VARCHAR(50) NOT NULL,
col_3 DATE NOT NULL,
col_4 INT(11) NOT NULL,
col_5 INT(11) NOT NULL,
col_6 FLOAT NOT NULL,
col_7 TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (col_1, col_2, col_3)
);

我有什么: 此表将col_4的百分比值与col_6中的col_5相对应。 (col_5 / col4 * 100)。该表每天更新一次。 Col_7是更新的时间戳。我用以下代码填充了样本数据:

INSERT INTO table_1 (col_1, col_2, col_3, col_4, col_5, col_6, col_7)
VALUES('String_1', 'STRING_4', '2018-03-29', 22166, 13772, 62.1312, '2018-03-29 15:01:55'),
('String_1', 'StrIng_5', '2018-03-29', 22166, 512, 2.30984, '2018-03-29 15:01:55'),
('String_1', 'String_6', '2018-03-29', 22166, 3325, 15.0005, '2018-03-29 15:01:55'),
('STRING_2', 'STRING_4', '2018-03-29', 13118, 7329, 55.8698, '2018-03-29 15:02:03'),
('STRING_2', 'StrIng_5', '2018-03-29', 13118, 0, 0, '2018-03-29 15:02:03'),
('STRING_2', 'String_6', '2018-03-29', 13118, 109, 0.830919, '2018-03-29 15:02:03'),
('String_3', 'STRING_4', '2018-03-29', 334, 15, 4.49102, '2018-03-29 15:02:06'),
('String_3', 'StrIng_5', '2018-03-29', 334, 0, 0, '2018-03-29 15:02:06'),
('String_3', 'String_6', '2018-03-29', 334, 8, 2.39521, '2018-03-29 15:02:06'),
('String_1', 'STRING_4', '2018-03-30', 22166, 13778, 62.1583, '2018-03-30 04:00:15'),
('String_1', 'StrIng_5', '2018-03-30', 22166, 256, 1.15492, '2018-03-30 04:00:15'),
('String_1', 'String_6', '2018-03-30', 22166, 3325, 15.0005, '2018-03-30 04:00:15'),
('STRING_2', 'STRING_4', '2018-03-30', 13118, 7327, 55.8545, '2018-03-30 04:00:22'),
('STRING_2', 'StrIng_5', '2018-03-30', 13118, 0, 0, '2018-03-30 04:00:22'),
('STRING_2', 'String_6', '2018-03-30', 13118, 109, 0.830919, '2018-03-30 04:00:22'),
('String_3', 'STRING_4', '2018-03-30', 333, 14, 4.2042, '2018-03-30 04:00:25'),
('String_3', 'StrIng_5', '2018-03-30', 333, 0, 0, '2018-03-30 04:00:25'),
('String_3', 'String_6', '2018-03-30', 333, 8, 2.4024, '2018-03-30 04:00:25'),
('String_1', 'STRING_4', '2018-03-31', 22165, 13778, 62.1611, '2018-03-31 04:00:09'),
('String_1', 'StrIng_5', '2018-03-31', 22165, 512, 2.30995, '2018-03-31 04:00:09'),
('String_1', 'String_6', '2018-03-31', 22165, 3335, 15.0462, '2018-03-31 04:00:09'),
('STRING_2', 'STRING_4', '2018-03-31', 13119, 7327, 55.8503, '2018-03-31 04:00:17'),
('STRING_2', 'StrIng_5', '2018-03-31', 13119, 0, 0, '2018-03-31 04:00:17'),
('STRING_2', 'String_6', '2018-03-31', 13119, 112, 0.853724, '2018-03-31 04:00:17'),
('String_3', 'STRING_4', '2018-03-31', 333, 14, 4.2042, '2018-03-31 04:00:20'),
('String_3', 'StrIng_5', '2018-03-31', 333, 0, 0, '2018-03-31 04:00:20'),
('String_3', 'String_6', '2018-03-31', 333, 8, 2.4024, '2018-03-31 04:00:20');

我想做的事情是每周六运行一次SQL并返回4个完整周(周日 - 周六是一个完整的一周)并计算col_2的每个不同组合的col_6的每周平均值。 col_3。

select distinct col_2, col_1 from table_1 order by col_2, col_1;

因此,对于此示例数据,我试图得到如下输出: ExpectedResult

我尝试了什么: 我已经开始使用类似的东西给我平均所有过去的数据:

select count(distinct col_3) from table_1;

select col_2, col_1, round(avg(col_6))
  from table_1
 group by col_2, col_1;

我想我必须在第二个SQL的SELECT子句中添加CASE并在该CASE语句中使用第一个SQL。但无法弄清楚如何获得过去4周的每周平均值(4个数字)而不是单个平均值。

我的问题:如何使用单个/多个SQL获得我想要的结果?我可以使用单个SQL或多个。

1 个答案:

答案 0 :(得分:1)

这是我的目标,它将输出正确的数据,但不是您想要的格式。您需要首先调整输出,但这可能是您在呈现数据时可以更轻松的事情。

以下代码包含两周前的内容,但您可以轻松复制第二个选择并相应地增加DATE_SUB中的间隔

SELECT col_2, col_1, DATE_FORMAT(sysdate(), "%m/%d") as saturday, AVG(col_6)
FROM table_1
WHERE col_3 between DATE_SUB(sysdate(), interval 1 week) and sysdate()
GROUP BY col_2, col_1, saturday
UNION ALL
SELECT col_2, col_1, DATE_FORMAT(date_sub(sysdate(), interval 1 week), "%m/%d") as saturday, AVG(col_6)
FROM table_1
WHERE col_3 between DATE_SUB(sysdate(), interval 2 week) and date_sub(sysdate(), interval 1 week)
GROUP BY col_2, col_1, saturday