好吧,我有一个表,其中包含一些重复的列,但是时间并不完全相同,我需要做一个小组,范围为2小时。
请注意,所有标有黑色的字段都会重复,但是datetime字段可能会相差几分钟,因此我需要在两小时的间隔内加入所有人,组成一个小组。
类似于:SELECT * FROM tabela GROUP BY sinistro, prefixo, data
AND horaIfull_oco范围2h
结果也有必要,如果可能,这些列都带有其所有ID,例如:id1, id2
等
他将加入所有相距最多两个小时的人。 如果有人知道怎么做,我已经进行了研究,却一无所获。
答案 0 :(得分:1)
您可以选择floor(hour / 2)
,它会告诉您它属于哪个两个小时的时段,并按日期和日期进行分组:
SELECT *,
group_concat(id_oco) as grouped_ids,
floor(HOUR(horaIfull_oco) / 2) AS two_hour
FROM tabela
GROUP BY inclusao_oco, sinistro, prefixo, data, DATE(horaIfull_oco), two_hour
答案 1 :(得分:1)
GROUP BY FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)
({7200
是2个小时。)
如果要获取每个组的基准时间:
FROM_UNIXTIME(
FLOOR(UNIX_TIMESTAMP(horaIfull_oco) / 7200)
, 7200)
可以通过更改7200
将该技术应用于任何常规时间间隔。
答案 2 :(得分:1)
假定一个数据库表具有一个具有以下模式的表:
CREATE TABLE t1 (
c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
c2 VARCHAR(100),
c3 VARCHAR(100),
c4 DATE,
c5 TIME,
c6 DATETIME);
INSERT INTO t1 (c1,c2,c3,c4,c5,c6)
VALUES
(1,'DDA1123_12112018','55307','2018-12-11','00:43:00', '2018-12-11 00:43:00'),
(2,'DDA1123_12112018','55307','2018-12-11','00:07:00', '2018-12-11 00:07:00'),
(3,'DDA1124_12112018','55308','2018-12-11','03:07:00', '2018-12-11 03:07:00'),
(4,'DDA1124_12112018','55308','2018-12-11','04:07:00', '2018-12-11 04:07:00'),
(5,'DDA1124_12112018','55308','2018-12-11','07:07:00', '2018-12-11 07:07:00'),
(6,'DDA1125_12112018','55309','2018-12-11','06:07:00', '2018-12-11 06:07:00'),
(7,'DDA1125_12112018','55309','2018-12-11','07:57:00', '2018-12-11 07:57:00'),
(8,'DDA1126_12112018','55310','2018-12-11','08:07:00', '2018-12-11 08:07:00'),
(9,'DDA1127_12112018','55311','2018-12-11','12:07:00', '2018-12-11 12:07:00'),
(10,'DDA1127_12112018','55311','2018-12-11','14:07:00', '2018-12-11 14:07:00'),
(11,'DDA1127_12112018','55311','2018-12-11','17:07:00', '2018-12-11 17:07:00');
在这种情况下,您可以像在此示例查询中一样,在CASE ... WHEN
中使用GROUP BY
。
SELECT c1,c2,c3,c4,c5,c6,
CASE TRUE
WHEN HOUR(c6) BETWEEN 0 AND 2 THEN 0
WHEN HOUR(c6) BETWEEN 2 AND 4 THEN 2
WHEN HOUR(c6) BETWEEN 4 AND 6 THEN 4
WHEN HOUR(c6) BETWEEN 6 AND 8 THEN 6
WHEN HOUR(c6) BETWEEN 8 AND 10 THEN 8
WHEN HOUR(c6) BETWEEN 10 AND 12 THEN 10
WHEN HOUR(c6) BETWEEN 12 AND 14 THEN 12
WHEN HOUR(c6) BETWEEN 14 AND 16 THEN 14
WHEN HOUR(c6) BETWEEN 16 AND 18 THEN 16
WHEN HOUR(c6) BETWEEN 18 AND 20 THEN 18
WHEN HOUR(c6) BETWEEN 20 AND 22 THEN 20
WHEN HOUR(c6) BETWEEN 22 AND 24 THEN 22
END AS c7 FROM t1
GROUP BY c7;
您可以在SQL Fiddle中查看以上示例的结果。
编辑
为获得更好的结果,可以将上述查询重写:
SELECT c1,c2,c3,c4,c5,c6,
CASE TRUE
WHEN HOUR(c6) BETWEEN 0 AND 1 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 0
WHEN HOUR(c6) BETWEEN 2 AND 3 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 2
WHEN HOUR(c6) BETWEEN 4 AND 5 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 4
WHEN HOUR(c6) BETWEEN 6 AND 7 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 6
WHEN HOUR(c6) BETWEEN 8 AND 9 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 8
WHEN HOUR(c6) BETWEEN 10 AND 11 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 10
WHEN HOUR(c6) BETWEEN 12 AND 13 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 12
WHEN HOUR(c6) BETWEEN 14 AND 15 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 14
WHEN HOUR(c6) BETWEEN 16 AND 17 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 16
WHEN HOUR(c6) BETWEEN 18 AND 19 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 18
WHEN HOUR(c6) BETWEEN 20 AND 21 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 20
WHEN HOUR(c6) BETWEEN 22 AND 23 AND MINUTE(c6) BETWEEN 0 AND 59 THEN 22
END AS c7 FROM t1
GROUP BY c7;
您可以在此SQL Fiddle中查看编辑后的示例的结果。