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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:40:18+00:00 2026-05-10T15:40:18+00:00

I have a table containing prices for a lot of different things in a

  • 0

I have a table containing prices for a lot of different ‘things’ in a MS SQL 2005 table. There are hundreds of records per thing per day and the different things gets price updates at different times.

ID uniqueidentifier not null, ThingID int NOT NULL, PriceDateTime datetime NOT NULL, Price decimal(18,4) NOT NULL 

I need to get today’s latest prices for a group of things. The below query works but I’m getting hundreds of rows back and I have to loop trough them and only extract the latest one per ThingID. How can I (e.g. via a GROUP BY) say that I want the latest one per ThingID? Or will I have to use subqueries?

SELECT *  FROM Thing WHERE ThingID IN (1,2,3,4,5,6)   AND PriceDate > cast( convert(varchar(20), getdate(), 106) as DateTime)  

UPDATE: In an attempt to hide complexity I put the ID column in a an int. In real life it is GUID (and not the sequential kind). I have updated the table def above to use uniqueidentifier.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T15:40:19+00:00Added an answer on May 10, 2026 at 3:40 pm

    I think the only solution with your table structure is to work with a subquery:

    SELECT *    FROM Thing    WHERE ID IN (SELECT max(ID) FROM Thing                     WHERE ThingID IN (1,2,3,4)                    GROUP BY ThingID) 

    (Given the highest ID also means the newest price)

    However I suggest you add a ‘IsCurrent’ column that is 0 if it’s not the latest price or 1 if it is the latest. This will add the possible risk of inconsistent data, but it will speed up the whole process a lot when the table gets bigger (if it is in an index). Then all you need to do is to…

    SELECT *    FROM Thing    WHERE ThingID IN (1,2,3,4)      AND IsCurrent = 1 

    UPDATE

    Okay, Markus updated the question to show that ID is a uniqueid, not an int. That makes writing the query even more complex.

    SELECT T.*     FROM Thing T    JOIN (SELECT ThingID, max(PriceDateTime)             WHERE ThingID IN (1,2,3,4)             GROUP BY ThingID) X ON X.ThingID = T.ThingID                                  AND X.PriceDateTime = T.PriceDateTime    WHERE ThingID IN (1,2,3,4) 

    I’d really suggest using either a ‘IsCurrent’ column or go with the other suggestion found in the answers and use ‘current price’ table and a separate ‘price history’ table (which would ultimately be the fastest, because it keeps the price table itself small).

    (I know that the ThingID at the bottom is redundant. Just try if it is faster with or without that ‘WHERE’. Not sure which version will be faster after the optimizer did its work.)

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

Sidebar

Related Questions

I have a table containing a number of timestamps per day, they represents start
I have a table containing view/click records. The time is stored in a unix
I have a table containing records of the date and time each product was
I have a table containing hundreds of entries and I am trying to delete
We have a table containing coordinates, among other things. We used to store these
I have a table containing a list of individual prices for a variety of
I'm currently working with MS SQL 2005, and have a table that has 17
I have table containing data . In every row there is a checkbox plus
I have a table containing the runtimes for generators on different sites, and I
I have a MySql table containing stock quotes (stock_symbol, quote_date, open_price, high_price, low_price, close_price)

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.