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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:14:16+00:00 2026-06-13T00:14:16+00:00

Possible Duplicate: How to get the record of a table who contains the maximum

  • 0

Possible Duplicate:
How to get the record of a table who contains the maximum value?

I’ve got an aggregate query like the following:

SELECT TrainingID, Max(CompletedDate) as CompletedDate, Max(Notes) as Notes     --This will only return the longest notes entry
FROM HR_EmployeeTrainings ET
WHERE (ET.AvantiRecID IS NULL OR ET.AvantiRecID = @avantiRecID)
GROUP BY AvantiRecID, TrainingID            

Which is working, and returns correct data most of the time, but I noticed a problem. The Notes field which gets returned will not necessarily match the record that the max(completedDate) is from. Instead it will be the one with the longest string? Or the one with the highest ASCII value? What does SQL Server do in the event of a tie between two records? I’m not even sure. What I want to get is the notes field from the max(completedDate) record. How should I got about doing this?

  • 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-13T00:14:17+00:00Added an answer on June 13, 2026 at 12:14 am

    You can use a subquery. The subquery will get the Max(CompletedDate). You then take this value and join on your table again to retrieve the note associate with that date:

    select ET1.TrainingID,
      ET1.CompletedDate,
      ET1.Notes
    from HR_EmployeeTrainings ET1
    inner join
    (
      select Max(CompletedDate) CompletedDate, TrainingID
      from HR_EmployeeTrainings
      --where AvantiRecID IS NULL OR AvantiRecID = @avantiRecID
      group by TrainingID
    ) ET2
      on ET1.TrainingID = ET2.TrainingID
      and ET1.CompletedDate = ET2.CompletedDate
    where ET1.AvantiRecID IS NULL OR ET1.AvantiRecID = @avantiRecID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Select Records multiple times from table I want to have my query
Possible Duplicate: Parent Child table record - Building SQL query Here is my table
Possible Duplicate: How to get random record from MS Access database In my project
Possible Duplicate: Get ID of last inserted record in oracle db I am brand
Possible Duplicate: How to select all records from table apart from the last 100
Possible Duplicate: AVAudioRecorder doesn't record while screen is locked I'm trying to get my
Possible Duplicate: Get just the Date from grouping in select from DateTime column in
Possible Duplicate: get the value of an url response with curl I have an
Possible Duplicate: Get and Parse CSV file in android I would like to store
Possible Duplicate: SQL: Find the max record per group I have a table with

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.