我有一张名为" powerpro"包括" power"记录在当天每一分钟的数据如下:(假设包括一个月的记录)
+---------------------+---------------+---------------+---------------+
| date_time | phase_1_power | phase_2_power | phase_3_power |
+---------------------+---------------+---------------+---------------+
| 2014/12/01 00:00:00 | 73.0767 | -68.2627 | -73.0767 |
| 2014/12/01 00:01:00 | 73.0293 | -68.3134 | -73.0293 |
| 2014/12/01 00:02:00 | 72.9819 | -68.3641 | -72.9819 |
| 2014/12/01 00:03:00 | 72.9345 | -68.4148 | -72.9345 |
| 2014/12/01 00:04:00 | 72.8871 | -68.4655 | -72.8871 |
| 2014/12/02 00:00:00 | 72.8397 | -68.5162 | -72.8397 |
| 2014/12/02 00:01:00 | 72.7923 | -68.5669 | -72.7923 |
| 2014/12/02 00:02:00 | 72.7449 | -68.6176 | -72.7449 |
| 2014/12/02 00:03:00 | 72.6975 | -68.6683 | -72.6975 |
| 2014/12/02 00:04:00 | 72.6501 | -68.7119 | -72.6501 |
| 2014/12/03 00:00:00 | 72.6027 | -68.7697 | -72.6027 |
| 2014/12/03 00:01:00 | 72.5553 | -68.8204 | -72.5553 |
| 2014/12/03 00:02:00 | 72.5079 | -68.8711 | -72.5079 |
| 2014/12/03 00:03:00 | 72.4605 | -68.9218 | -72.4605 |
| 2014/12/03 00:04:00 | 72.4131 | -68.9725 | -72.4131 |
+---------------------+---------------+---------------+---------------+
我想得到平衡 结束时间UNIX_TIMESTAMP()value -start time UNIX_TIMESTAMP()值AND 结束时间=开始时间UNIX_TIMESTAMP()值+ UNIX_TIMESTAMP()下一个第7个日期的值
我试过了:
SELECT UNIX_TIMESTAMP(a1.date_time) AS time_reff, a1.phase_1_power AS ph1, a1.phase_2_power AS ph2, a1.phase_3_power AS ph3
FROM powerpro a1
JOIN (SELECT UNIX_TIMESTAMP(date_time) time, MIN(UNIX_TIMESTAMP(date_time)) AS min
FROM powerpro
GROUP BY date_time
)a2 ON a1.date_time = a2.min
JOIN (SELECT UNIX_TIMESTAMP(date_time) time, MIN(UNIX_TIMESTAMP(date_time)) AS min
FROM powerpro
GROUP BY date_time
) a3 ON a1.date_time = a3.time - INTERVAL 7 DAY
WHERE a1.date_time BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW() ORDER BY a1.date_time
但只有空洞的结果。
任何人都可以帮助我吗?感谢
答案 0 :(得分:0)
删除WHERE
部分:它会将结果集限制为仅在NOW()
的7天内开始日期的条目。同时,由于JOIN
s,您只输出一对开始时间 - 结束时间间隔为7天。结合起来,这意味着唯一可以输出的行是结束时间为NOW()
且开始时间为DATE_SUB(NOW(), INTERVAL 7 DAY)
的行。
当您删除WHERE
部分时,您将获得所有开始时间 - 结束时间间隔为7天。
更新1:
由于这仍然不起作用,让我们尝试逐个重建此查询。
首先尝试这个:
SELECT UNIX_TIMESTAMP(date_time) time, MIN(UNIX_TIMESTAMP(date_time)) AS min
FROM powerpro
GROUP BY DATE(date_time)
这应该是每天最早的时间戳。
更新2:
现在尝试:
SELECT UNIX_TIMESTAMP(a1.date_time) AS time_reff, a1.phase_1_power AS ph1, a1.phase_2_power AS ph2, a1.phase_3_power AS ph3
FROM powerpro a1
JOIN (SELECT UNIX_TIMESTAMP(date_time) time, MIN(UNIX_TIMESTAMP(date_time)) AS min
FROM powerpro
GROUP BY DATE(date_time)
)a2 ON time_reff = a2.min
这应该为每天的最早时间戳提供所有阶段。
更新3:
SELECT UNIX_TIMESTAMP(a1.date_time) AS time_reff, a1.phase_1_power AS ph1, a1.phase_2_power AS ph2, a1.phase_3_power AS ph3
FROM powerpro a1
JOIN (SELECT UNIX_TIMESTAMP(date_time) time, MIN(UNIX_TIMESTAMP(date_time)) AS min
FROM powerpro
GROUP BY DATE(date_time)
)a2 ON UNIX_TIMESTAMP(a1.date_time) = a2.min
JOIN (SELECT UNIX_TIMESTAMP(date_time) time, MIN(UNIX_TIMESTAMP(date_time)) AS min
FROM powerpro
GROUP BY DATE(date_time)
)a3 ON CAST(a1.date_time AS DATETIME) = DATE_SUB(a3.time, INTERVAL 1 DAY)
更新4:
SELECT UNIX_TIMESTAMP(a1.date_time) AS time_reff, a1.phase_1_power AS ph1, a1.phase_2_power AS ph2, a1.phase_3_power AS ph3
FROM powerpro a1
JOIN (SELECT UNIX_TIMESTAMP(date_time) time, MIN(UNIX_TIMESTAMP(date_time)) AS min
FROM powerpro
GROUP BY DATE(date_time)
)a2 ON UNIX_TIMESTAMP(a1.date_time) = a2.min
JOIN (SELECT UNIX_TIMESTAMP(date_time) time, MIN(UNIX_TIMESTAMP(date_time)) AS min
FROM powerpro
GROUP BY DATE(date_time)
)a3 ON UNIX_TIMESTAMP(a1.date_time) = a3.min - 86400