0

鉴于下面的示例数据,我如何才能每分钟只获取一行time

然后我需要做出这样的条件:

SELECT IF (`Flag` IS NOT NULL,
           [fetch flag row],
           [otherwise fetch one row from that minute]);

样本数据

ID  Rate    Flag        time 
1   68.65    null       2012-04-18 10.00.01
2   68.63    null       2012-04-18 10.00.11
3   68.68    null       2012-04-18 10.00.21
4   68.70    null       2012-04-18 10.00.31
5   68.71    null       2012-04-18 10.00.41
6   68.60    null       2012-04-18 10.00.51
7   68.64    null       2012-04-18 10.01.01
8   68.75    null       2012-04-18 10.01.11
9   68.72    1          2012-04-18 10.01.21
10  68.63    null       2012-04-18 10.01.31
11  68.62    null       2012-04-18 10.01.41
12  68.60    null       2012-04-18 10.01.51

Expected Output
1   68.65    null       2012-04-18 10.00.01
9   68.72    1          2012-04-18 10.01.21

这是我到目前为止所得到的:

SELECT FLAG as signal, DATE_FORMAT(TIME, '%Y-%m-%d %H:%i') as time, RATE as rate
       FROM RATES WHERE (FLAG!='' || (DATE_FORMAT(TIME, '%s')>50 ))
   GROUP BY time
   ORDER BY time DESC,FLAG DESC;
4

1 回答 1

0

您的问题可以描述为:在同一分钟分组的每组行中找到具有最大标志的行,如果多行具有相同的标志,则选择任何行。所以查询应该如下所示:

SELECT r.id, r.rate, rg.flag, rg.minute
FROM
  ( SELECT MAX(flag) as flag , DATE_FORMAT(`TIME`, '%Y-%m-%d %H:%i') as minute
     FROM RATES
     GROUP BY minute ) rg
JOIN RATES r
ON r.id =(SELECT ru.id FROM RATES ru
          WHERE ru.flag <=> rg.flag
          AND ru.time BETWEEN CONCAT(rg.minute,':00') AND CONCAT(rg.minute,':59')
          LIMIT 1)
于 2012-04-18T01:27:31.830 回答