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

The Archive Base Latest Questions

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

I have a History Table in SQL Server that basically tracks an item through

  • 0

I have a History Table in SQL Server that basically tracks an item through a process. The item has some fixed fields that don’t change throughout the process, but has a few other fields including status and Id which increment as the steps of the process increase.

Basically I want to retrieve the last step for each item given a Batch Reference. So if I do a

Select * from HistoryTable where BatchRef = @BatchRef 

It will return all the steps for all the items in the batch – eg

 Id      Status  BatchRef        ItemCount 1       1       Batch001        100 1       2       Batch001        110 2       1       Batch001        60 2       2       Batch001        100 

But what I really want is:

 Id      Status  BatchRef        ItemCount 1       2       Batch001        110 2       2       Batch001        100 

Edit: Appologies – can’t seem to get the TABLE tags to work with Markdown – followed the help to the letter, and looks fine in the preview

  • 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. 2026-05-10T15:12:07+00:00Added an answer on May 10, 2026 at 3:12 pm

    It’s kind of hard to make sense of your table design – I think SO ate your delimiters.

    The basic way of handling this is to GROUP BY your fixed fields, and select a MAX (or MIN) for some unqiue value (a datetime usually works well). In your case, I think that the GROUP BY would be BatchRef and ItemCount, and Id will be your unique column.

    Then, join back to the table to get all columns. Something like:

    SELECT *  FROM HistoryTable JOIN (    SELECT         MAX(Id) as Id.        BatchRef,        ItemCount    FROM HsitoryTable    WHERE        BacthRef = @batchRef    GROUP BY        BatchRef,        ItemCount  ) as Latest ON    HistoryTable.Id = Latest.Id 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 104k
  • Answers 104k
  • 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 no you can't do this, mono-debug is linux only.. (there's… May 11, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer Full Text is going to work off of the search… May 11, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer This line in C++ is a forward declaration. It's stating… May 11, 2026 at 8:32 pm

Related Questions

I have a trigger in a table in SQL Server 2000, this is the
Is there a built in way in SQL Server 2005 to audit things specifically
I have a medical database that keeps different types of data on patients: examinations,
A new client of mine has a small VB/Access database application written in 2002

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.