简体   繁体   English

选择一组最近的ID,数量FROM表格,其中ID多次出现

[英]SELECT set of most recent id, amount FROM table, where id occurs many times

I have a table recording the amount of data transferred by a given service on a given date. 我有一张表,记录着给定服务在给定日期传输的数据量。 One record is entered daily for a given service. 每天为给定服务输入一条记录。

I'd like to be able to retrieve the most recent amount for a set of services. 我希望能够检索一组服务的最新amount

Example data set: 数据集示例:

serviceId | amount | date
-------------------------------
1         | 8      | 2010-04-12   
2         | 11     | 2010-04-12  
2         | 14     | 2010-04-11  
3         | 9      | 2010-04-11  
1         | 6      | 2010-04-10  
2         | 5      | 2010-04-10  
3         | 22     | 2010-04-10  
4         | 17     | 2010-04-19

Desired response (service ids 1,2,3): 所需的响应(服务ID 1,2,3):

serviceId | amount | date
-------------------------------
1         | 8      | 2010-04-12   
2         | 11     | 2010-04-12  
3         | 9      | 2010-04-11 

Desired response (service ids 2, 4): 所需的响应(服务ID 2、4):

serviceId | amount | date
-------------------------------
2         | 11     | 2010-04-12  
4         | 17     | 2010-04-19

This retrieves the equivalent as running the following once per serviceId: 这将检索等效结果,即每个serviceId运行一次以下命令:

SELECT serviceId, amount, date
FROM table
WHERE serviceId = <given serviceId>
ORDER BY date DESC
LIMIT 0,1

I understand how I can retrieve the data I want in X queries. 我了解如何在X查询中检索所需的数据。 I'm interested to see how I can retrieve the same data using either a single query or at the very least less than X queries. 我很想知道如何使用单个查询或至少少于X个查询来检索相同的数据。

I'm very interested to see what might be the most efficient approach. 我非常想知道什么是最有效的方法。 The table currently contains 28809 records. 该表当前包含28809条记录。

I appreciate that there are other questions that cover selecting the most recent set of records. 我很欣赏还有其他问题涉及选择最新记录集。 I have examined three such questions but have been unable to apply the solutions to my problem. 我已经检查了三个这样的问题,但是无法将解决方案应用于我的问题。

select m.*
from (
    select serviceId, max(date) as MaxDate
    from MyTable
    group by serviceId
) mm
inner join MyTable m on mm.serviceId = m.serviceId and mm.MaxDate = m.date

If you wish to filter by serviceId, you can do: 如果希望按serviceId进行过滤,则可以执行以下操作:

select m.*
from (
    select serviceId, max(date) as MaxDate
    from MyTable
    where serviceId in (1, 2, 3)
    group by serviceId
) mm
inner join MyTable m on mm.serviceId = m.serviceId and mm.MaxDate = m.date
SELECT serviceId, amount, date 
  FROM table as t
  WHERE NOT EXIST (
    SELECT * FROM table as x 
       WHERE t.serviceId = x.serviceID AND t.date < x.date
  )

if you want to filter out some serviceIds than 如果您想过滤掉一些serviceIds

SELECT serviceId, amount, date 
  FROM table as t
  WHERE NOT EXIST (
    SELECT * FROM table as x 
       WHERE t.serviceId = x.serviceID AND t.date < x.date
  ) 
  AND serviceId in (2, 4)

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

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