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 8684221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:15:10+00:00 2026-06-12T22:15:10+00:00

Ok so I know you can’t pull specific fields without an aggregate when you

  • 0

Ok so I know you can’t pull specific fields without an aggregate when you perform a SQL command with a group by, but it seems to me that if you are doing an aggregate on a primary key that is guaranteed to be unique, there should be a way to pull the other rows of that column along with it. Something like this:

SELECT Max(id), 
       foo, 
       bar 
FROM   mytable 
GROUP  BY value1, 
          value2 

So ID is guaranteed to be unique, so it will have exactly 1 value for foo and bar, is there a way to generate a query like this?

I have tried this, but MyTable in this case has millions of rows, and the run-time for this is unacceptable:

SELECT * 
FROM   mytable 
WHERE  id IN (SELECT Max(id) 
              FROM   mytable 
              GROUP  BY value1, 
                        value2) 
       AND ...

Ideally I would like a solution that works at least as far back as SQL server 2005, but if there are better solutions in the later versions I would like to hear them as well.

  • 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-06-12T22:15:11+00:00Added an answer on June 12, 2026 at 10:15 pm

    Make sure you have an index defined such as:

    CREATE NONCLUSTERED INDEX idx_GroupBy ON mytable (value1, value2)
    

    IN can be tend to be slow if you have many rows. It may help to turn your IN into an INNER JOIN.

    SELECT
        data.*
    FROM
        mytable data
            INNER JOIN
        (
            SELECT id = MAX(id) 
            FROM mytable 
            GROUP BY value1, 
                     value2
        ) ids
            ON data.id = ids.id
    

    Unfortunately, Sql Server does not have any features that will do this any better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that I know can be done using a subselect, but
Sometimes, I'll end up having to catch an exception that I know can never
You can know if the event stack is empty calling the gtk.events_pending() method, but
I know you can not set a key value dynamically, but what about the
I know you can share control easily with master page. But what if I
As we all know that cakephp has default save command for inserting record into
I'm trying to do something that I dont know how to do, but I
may i know can we specify url for http-basic so that only authenticate if
I see that there are two options that I know can be used in
Just wanted to know can we create web service with Axis2 without using eclipse

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.