如何在一天结束时获取数据

时间:2016-04-07 03:05:33

标签: mysql sql

如何从当天的最后一个end_date时间获取deep_md_msl需要一些帮助。

例如这个日期:

2014-10-15 last depth_md_msl is 41.98 or 

2014-10-16 last depth_md_msl of the day is 109.98

当我使用以下查询时,它返回任何内容并且它完全为空

SELECT depth_md_msl 
FROM fa
WHERE fa.start_datetime >= CURDATE() - INTERVAL 1 DAY 



end_datetime         depth_md_msl  
-------------------  --------------

2014-10-15 08:00:00           41.98
2014-10-15 08:00:00          (NULL)
2014-10-15 10:00:00          (NULL)
2014-10-15 10:00:00           41.98
2014-10-15 10:30:00           41.98
2014-10-15 10:30:00          (NULL)
2014-10-15 12:00:00           41.98
2014-10-15 12:00:00          (NULL)
2014-10-15 17:00:00           41.98
2014-10-15 17:00:00          (NULL)
2014-10-15 23:00:00           41.98
2014-10-15 23:45:00           41.98
2014-10-15 23:59:59           41.98
2014-10-16 00:30:00           41.98
2014-10-16 03:15:00           41.98
2014-10-16 12:00:00          106.98
2014-10-16 12:30:00          106.98
2014-10-16 14:00:00          109.98
2014-10-16 15:00:00          109.98
2014-10-16 19:00:00          109.98
2014-10-16 23:59:59          109.98
2014-10-17 06:00:00          109.98
2014-10-17 08:00:00          109.98
2014-10-17 15:30:00          109.98
2014-10-17 19:30:00          109.98
2014-10-17 23:59:59          109.98

2 个答案:

答案 0 :(得分:1)

我认为您需要将表格加入到自身中。

 SELECT A.end_datetime, A.depth_md_msl FROM fact_activity AS A
    JOIN (
         SELECT DATE(end_datetime) d, MAX(TIME(end_datetime)) t FROM fact_activity 
        GROUP BY 1
    ) B ON
     DATE(A.end_datetime) = B.d AND
     TIME(A.end_datetime) = B.t

答案 1 :(得分:0)

SELECT x.*
  FROM fa x
  JOIN (SELECT MAX(end_datetime) max_end_datetime FROM fa GROUP BY DATE(end_datetime)) y
    ON y.max_end_datetime = x.end_datetime;