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

  • Home
  • SEARCH
  • 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 335671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:10:14+00:00 2026-05-12T10:10:14+00:00

I have a performance issue when selecting data in my project. There is a

  • 0

I have a performance issue when selecting data in my project.

There is a table with 3 columns: “id”,”time” and “group”

  • The ids are just unique ids as usual.
  • The time is the creation date of the entry.
  • The group is there to cummulate certain entries together.

So the table data may look like this:

ID | TIME      | GROUP
------------------------
1  | 20090805  | A
2  | 20090804  | A
3  | 20090804  | B
4  | 20090805  | B
5  | 20090803  | A
6  | 20090802  | B

…and so on.

The task is now to select the “current” entries (their ids) in each group for a given date. That is, for each group find the most recent entry for a given date.

Following preconditions apply:

  • I do not know the different groups in advance – there may be many different ones changing over time
  • The selection date may lie “in between” the dates of the entries in the table. Then I have to find the closest one in each group. That is, TIME is less than the selection date but the maximum of those to which this rule applies in a group.

What I currently do is a multi-step process which I would like to change into single SELECT statement:

  1. SELECT DISTINCT group FROM table to find the available groups
  2. For each group found in 1), SELECT * FROM table WHERE time<selectionDate AND group=loop ORDER BY time DESC
  3. Take the first row of each result found in 2)

Obviously this is not optimal.

So I would be very happy if some more experienced SQL expert could help me to find a solution to put these steps in a single statement.

Thank you!

  • 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-12T10:10:14+00:00Added an answer on May 12, 2026 at 10:10 am

    The following will work on SQL Server 2005+ and Oracle 9i+:

    WITH groups AS (
           SELECT t.group,
                  MAX(t.time) 'maxtime'
             FROM TABLE t
         GROUP BY t.group)
    SELECT t.id,
           t.time,
           t.group
      FROM TABLE t
      JOIN groups g ON g.group = t.group AND g.maxtime = t.time
    

    Any database should support:

    SELECT t.id,
           t.time,
           t.group
      FROM TABLE t
      JOIN (SELECT t.group,
                   MAX(t.time) 'maxtime'
              FROM TABLE t
          GROUP BY t.group) g ON g.group = t.group AND g.maxtime = t.time
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 162k
  • Answers 163k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Table: size_selection Item_id numeric Item_size varchar Seq numeric Item_id ->… May 12, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer In Silverlight, only subclasses of FrameworkElement (not DependencyObject) can have… May 12, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer the last sp of vs 2008 seems to have magically… May 12, 2026 at 12:06 pm

Related Questions

I have created an MS Access 2003 application, set up as a split front-end/back-end
EDIT: I have added Slug column to address performance issues on specific record selection.
I'm working on a game in XNA for Xbox 360. The game has 3D
Background: I'm using the SlimDX C# wrapper for DirectX, and am drawing many 2D
I'm using ASP.NET, with session state stored out of process in SQL Server. When

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.