Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 726103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:24:50+00:00 2026-05-14T06:24:50+00:00

I have a table recording the amount of data transferred by a given service

  • 0

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.

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):

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

Desired response (service ids 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:

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. 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.

I’m very interested to see what might be the most efficient approach. The table currently contains 28809 records.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:24:51+00:00Added an answer on May 14, 2026 at 6:24 am
    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:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of data recording certain user events. The results looks something
I've got a table recording views of programs. Each program can have two different
If I have a table like: CREATE TABLE FRED ( recordId number(18) primary key,
I have table definition like below: Place (id, name) Review (id, userid, placeid) Favorite
I have the following table: SQL> desc recording Name Null? Type -------------------- -------- ------
I have a task to store large amount of gps data and some extra
I have a MYSQL table recording hits to a website it is of the
I have question regarding the storage of large amount of data. The situation is
I have a SQL Server 2008 database with a history table for recording changes
I have a table widget in reporting services where I group rows on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.