简体   繁体   English

SQL查询返回查询中的最高日期值

[英]SQL Query return highest date value in query

Im trying to run a query to do the following: 我试图运行查询来执行以下操作:
return all of the most recent entries in the table between a given date range. 返回给定日期范围内表中的所有最新条目。
Currently I am using this query 目前我正在使用此查询

SELECT id FROM schedule WHERE eventdate BETWEEN '2014-09-01' AND '2014-09-07'

Which returns records 1,2,3,4,5 from the schedule table thats shown below 它返回下面显示的计划表中的记录1,2,3,4,5

schedule table. 时间表。

+----+------------+-------------+-----------------+------------+----------+
| id | eventdate  | resource_id |      text       |  added_on  | added_by |
+----+------------+-------------+-----------------+------------+----------+
|  1 | 2014-09-05 |           1 | Some old text   | 2014-08-01 | Sam      |
|  2 | 2014-09-05 |           1 | Some newer text | 2014-09-01 | Jordan   |
|  3 | 2014-09-06 |           1 | another day     | 2014-09-03 | Jordan   |
|  4 | 2014-09-05 |           1 | Most recent     | 2014-09-10 | Jordan   |
|  5 | 2014-09-07 |           2 | Other resource  | 2014-09-09 | Sam      |
+----+------------+-------------+-----------------+------------+----------+

I'm trying to only return the unique records within the specified date range, where the unique records that should be returned are those with the highest date timestamp in the added_on column. 我试图只返回指定日期范围内的唯一记录,其中应返回的唯一记录是在added_on列中具有最高日期时间戳的added_on

In the example above I would like for only records 3,4,5 to be returned. 在上面的例子中,我想只返回记录3,4,5。 Records 1 and 2 have been superseded by record 4. 记录1和2已被记录4取代。

**Please note: The added_on column is of type date timestamp (yyyy-mm-dd HH:mm:ss) and has been left off for clarity ** **请注意: added_on列的类型为日期时间戳(yyyy-mm-dd HH:mm:ss),为清晰起见,已将其保留为**

I do not know the number of rows the query will return, the information that determines the uniqueness of the record is the eventdate, resource_id and added_on 我不知道查询将返回的行数,确定记录唯一性的信息是eventdate, resource_id and added_on
ie there should only be one record returned per resource per day, and this record should have the highest added_on value 即,每天每个资源只应返回一条记录,并且此记录应具有最高的added_on值

SELECT s1.* FROM schedule s1  
inner join (select max(id) as id1 from schedule WHERE eventdate BETWEEN '2014-09-01' AND '2014-09-07' group by eventdate,resource_id ) as s2 on s2.id1=s1.id 

It will work for you 它会对你有用

NOTE: 注意:

you said you determine the uniqueness of each day by the three columns eventdate, resource_id, and added_on... but you only want the greatest added_on per day... so do not group by added_on.. group by eventdate to get data by day, group by eventdate, resource_id to get data by day per resource_id. 你说你通过三列eventdate,resource_id和added_on来确定每一天的唯一性......但是你只想要每天最大的added_on ...所以不要按add_on .. group by eventdate分组来获取白天的数据,按eventdate分组,resource_id按资源_id每天获取数据。

try getting the unique id's and then filter an outer query by them 尝试获取唯一ID,然后通过它们过滤外部查询

SELECT * 
FROM schedule
WHERE ID IN
(   SELECT MAX(id) 
    FROM schedule 
    WHERE eventdate BETWEEN '2014-09-01' AND '2014-09-07' 
    GROUP BY eventdate, resource_id
)

DEMO1 demo1的

or if you want to use the highest added_on you can do it the same way 或者如果你想使用最高的added_on,你可以用同样的方式

SELECT * 
FROM schedule
WHERE added_on IN
(   SELECT MAX(added_on) 
    FROM schedule 
    WHERE eventdate BETWEEN '2014-09-01' AND '2014-09-07' 
    GROUP BY eventdate, resource_id
)

DEMO2 DEMO2

if you want to get rid of using IN and use a JOIN you can do it with a JOIN on MAX() added on. 如果你想摆脱使用IN并使用JOIN,你可以在MAX()上添加一个JOIN来实现。

SELECT * 
FROM schedule s
JOIN 
(   SELECT MAX(added_on) as added_on 
    FROM schedule
    WHERE eventdate BETWEEN '2014-09-01' AND '2014-09-07' 
    GROUP BY eventdate, resource_id
) t ON t.added_on = s.added_on

DEMO3 DEMO3

You need to get the latest record for every unique combination of eventdate, resource_id, added_on, like this: 您需要获取eventdate,resource_id,added_on的每个唯一组合的最新记录,如下所示:

SELECT schedule.* 
FROM schedule JOIN (
   SELECT MAX(id) AS max_id 
   FROM schedule
   GROUP BY eventdate, resource_id, added_on
) t
ON t.max_id = schedule.id
WHERE eventdate BETWEEN '2014-09-01' AND '2014-09-07'

I think this will be the simplest query you can have. 我认为这将是您可以拥有的最简单的查询。 Just comment here if you have further question. 如果您还有其他问题,请在此处发表评论

 SELECT MAX(id) , eventdate, Max(added_on) FROM schedule GROUP BY eventdate.

This will return 这将返回

+----+------------+------------+
| id | eventdate  |  added_on  | 
+----+------------+------------+
|  3 | 2014-09-06 | 2014-09-03 | 
|  4 | 2014-09-05 | 2014-09-10 | 
|  5 | 2014-09-07 | 2014-09-09 | 
+----+------------+------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM