1

我有这个查询:

SELECT t1.* FROM table t1
  JOIN 
  (
    SELECT MIN(orig_id) min_orig_id, MAX(orig_id) max_orig_id 
    FROM table
    GROUP BY left(datetime,4), mid(datetime,5,2), 
        mid(datetime,7,2), mid(datetime,10,2), mid(datetime,13,2)
  ) t2
  ON t1.orig_id = t2.min_orig_id OR t1.orig_id = t2.max_orig_id; 

它按每分钟的第一条和最后一条记录对记录进行分组。现在我想每 3 分钟按第一条和最后一条记录分组。任何想法?谢谢

4

1 回答 1

0

这可能是您正在寻找的

SELECT t1.* FROM table t1
  JOIN 
  (
    SELECT MIN(orig_id) min_orig_id, MAX(orig_id) max_orig_id 
    FROM table
    GROUP BY concat(left(datetime,4), mid(datetime,5,2), 
        mid(datetime,7,2), mid(datetime,10,2),(cast mid(datetime,13,2) as integer)/3)
  ) t2
  ON t1.orig_id = t2.min_orig_id OR t1.orig_id = t2.max_orig_id; 

如果 datetime 是时间戳/日期时间数据类型而不是字符串

SELECT t1.* FROM table t1
  JOIN 
  (
    SELECT MIN(orig_id) min_orig_id, MAX(orig_id) max_orig_id 
    FROM table
    GROUP BY UNIX_TIMESTAMP(datetime)/3
  ) t2
  ON t1.orig_id = t2.min_orig_id OR t1.orig_id = t2.max_orig_id; 

干净多了

于 2012-09-02T01:30:46.073 回答