我有下表。在2.5分鐘(150秒)的情況下,兩個工廠有一個記錄。我想得到一個結果,顯示各工廠的最大值(日期時間)和最小值(日期時間)。但是,對於每個國家的變化,都應該有新的路線。組狀態相同,直到狀態改變
plant state unix_timest datetime
1 1 1452639750 2016-01-13 00:02:30
2 1 1452639750 2016-01-13 00:02:30
1 1 1452639900 2016-01-13 00:05:00
2 1 1452639900 2016-01-13 00:05:00
1 1 1452640050 2016-01-13 00:07:30
2 1 1452640050 2016-01-13 00:07:30
1 1 1452640200 2016-01-13 00:10:00
2 1 1452640200 2016-01-13 00:10:00
1 1 1452640350 2016-01-13 00:12:30
2 0 1452640350 2016-01-13 00:12:30
1 1 1452640500 2016-01-13 00:15:00
2 0 1452640500 2016-01-13 00:15:00
1 1 1452640650 2016-01-13 00:17:30
2 1 1452640650 2016-01-13 00:17:30
1 1 1452640800 2016-01-13 00:20:00
2 1 1452640800 2016-01-13 00:20:00
我的SQL語句是:
SELECT a.line, a.current_state, min(a.`datetime`) AS mindate, max(a.`datetime`) AS maxdate
FROM `bde_production`
LEFT JOIN bde_production a ON a.unix_timestamp = bde_production.unix_timestamp
LEFT JOIN bde_production b ON b.unix_timestamp = bde_production.unix_timestamp -150
GROUP BY a.line, a.current_state ORDER BY a.line, mindate
結果:
plant current_state mindate maxdate
1 1 2016-01-13 00:02:30 2016-01-13 00:20:00
2 1 2016-01-13 00:02:30 2016-01-13 00:20:00
2 0 2016-01-13 00:12:30 2016-01-13 00:15:00
我希望得到什麼:
plant current_state mindate maxdate
1 1 2016-01-13 00:02:30 2016-01-13 00:20:00
2 1 2016-01-13 00:02:30 2016-01-13 00:10:00
2 0 2016-01-13 00:12:30 2016-01-13 00:15:00
2 1 2016-01-13 00:17:30 2016-01-13 00:20:00
感謝