特定日期后如何选择MAX(日期)

时间:2018-10-15 09:04:19

标签: mysql sql

背景

我正在编写Python脚本以获取一些数据。我在SQL数据库中有2个表engine_hoursmachines。我想获取过去2天收到的最新数据(已完成date_recorded >=)。

表格:

第一个表:

engine_hours
=============
machine_id       
date_recorded
value

第二张表:

machines
========
id
title

样本数据表:

第一个表:

                    engine_hours
==================================================
 machine_id  |      date_recorded       | value
-------------+--------------------------+---------       
     1       |   16/10/2018  20:30:02   |   10
     3       |   16/10/2018  19:02:32   |   42
     2       |   16/10/2018  20:32:56   |   13
     2       |   16/10/2018  19:23:23   |   12
     1       |   16/10/2018  16:54:59   |   10
     1       |   16/10/2018  16:52:59   |   10
     1       |   14/10/2018  10:24:59   |   10

第二张表:

    machines
==================
  id  |   title
------+-----------   
   1  |  ABC-123
   2  |  DEF-456  
   3  |  GHI-789

所需的输出:

=============================================================
 machine_id  |  title  |       date_recorded      | value    
     1       | ABC-123 |   16/10/2018  20:30:02   |   10
     2       | DEF-456 |   16/10/2018  20:32:56   |   13
     3       | GHI-789 |   16/10/2018  19:02:32   |   42

我尝试过的事情:

我尝试了4个不同的查询,但失败了:

engine_hours_query = "SELECT ma.`title`, eh.`machine_id`, eh.`value`, eh.`date_recorded` " \
                     "FROM `engine_hours` AS eh inner join `machines` AS ma " \
                     "WHERE eh.`machine_id` IN ({}) AND eh.`date_recorded` >=  \"{}\" " \
                     " AND eh.`machine_id` = ma.`id`".format(", ".join([str(m_id) for m_id in list_of_machine_ids]),
                                                             cut_off_date)

engine_hours_query_2 = "SELECT `machine_id`, `value`, `date_recorded` FROM `engine_hours` AS eh " \
                       "WHERE `date_recorded` = ( SELECT MAX(`date_recorded`) " \
                       "FROM `engine_hours` AS eh2  " \
                       "WHERE eh.`machine_id` = eh2.`machine_id`)"


engine_hours_query_3 = "SELECT `machine_id`, `value`, `date_recorded` FROM `engine_hours` AS eh " \
                       "WHERE `date_recorded` = ( SELECT MAX(`date_recorded`) " \
                       "FROM `engine_hours` AS eh2 ) " \
                       "WHERE eh.`date_recorded` >=  \"{}\"".format(cut_off_date)

engine_hours_query_4 = "SELECT ma.`title`, eh.`machine_id`, eh.`value`, eh.`date_recorded` " \
                     "FROM `engine_hours` AS eh inner join `machines` AS ma " \
                     "WHERE eh.`machine_id` IN ({}) AND eh.`date_recorded` >=  \"{}\" " \
                     " AND eh.`machine_id` = ma.`id`".format(", ".join([str(m_id) for m_id in list_of_machine_ids]),
                                                             cut_off_date)

研究:

2 个答案:

答案 0 :(得分:1)

此版本应执行您想要的操作:

SELECT eh.machine_id, eh.value, eh.date_recorded
FROM engine_hours eh
WHERE eh.date_recorded = (SELECT MAX(eh2.date_recorded)
                          FROM engine_hours eh2
                          WHERE eh.machine_id = eh2.machine_id
                         );

如果仍然需要该条件,则可能要添加AND eh.date_recorded >= NOW() - INTERVAL 2 DAY

答案 1 :(得分:0)

您要从数据子集中选择MAX日期。该子集的标准是它的date_recorded大于2天前

SELECT MAX(eh.`date_recorded`)
FROM `engine_hours` eh
WHERE eh.`date_recorded` >= DATE_SUB(NOW(), INTERVAL 2 DAY);

取决于需求的复杂程度,您可能需要转而使用子查询,但是就目前而言,WHERE子句发生在MAX操作之前,因此此查询将所有数据限制为最近两天,然后给出其中的最大日期