根据下面的示例数据,如何在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;
感谢您的帮助。
尼基尔
答案 0 :(得分:0)
您的问题可以描述为:在按相同分钟分组的每组行中查找包含最大标记的行,如果多行具有相同的标记,请选择any。 因此查询应如下所示:
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)