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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:13:15+00:00 2026-06-17T03:13:15+00:00

I am trying to trying to update create a query that will sort data

  • 0

I am trying to trying to update create a query that will sort data by Date and time, find the newest record and then update another field in the record marking it as so.

Take my life harder the time and data are two separate fields and the time is also a string.

So here is what I have so far,

UPDATE server.ESCC_HWAY_ASSETS_GULLIES_N 
set CURRENT_REC = 'Y' 
From server.ESCC_HWAY_ASSETS_GULLIES_N A 
inner join ( 
SELECT GULLY_ID, Max([DATE]) AS MaxDate, MAX([TIME]) AS MaxTime 
FROM server.ESCC_HWAY_ASSETS_GULLIES_N B
GROUP BY GULLY_ID, [DATE] ) B 
on A.GULLY_ID = B.GULLY_ID and A.[DATE] = B.MaxDate and A.[TIME] = B.MaxTime

This results in data that is sorted by time and date but it updates all fields it finds, apart from on dates where there are two entries. Then it only updates the newest record.

I am testing on a single record – B47605 which gives the following results on this queuer

SELECT GULLY_ID, Max([DATE]) AS MaxDate, MAX([TIME]) AS MaxTime 
FROM ESMAPADMIN.ESCC_HWAY_ASSETS_GULLIES_N B
WHERE GULLY_ID = 'B47605'
GROUP BY GULLY_ID, [DATE]

Gully_ID     MaxDate                MaxTime
B47605  2008-03-12 00:00:00.000 09:02:29
B47605  2008-09-19 00:00:00.000 09:51:14
B47605  2009-02-16 00:00:00.000 11:18:28
B47605  2009-08-21 00:00:00.000 12:34:45
B47605  2010-03-16 00:00:00.000 09:22:26
B47605  2010-08-25 00:00:00.000 11:19:55
B47605  2011-03-07 00:00:00.000 12:19:56
B47605  2012-05-02 00:00:00.000 20:57:54

The result I would like is to only have the newest record returned so –

Gully_ID     MaxDate                MaxTime
B47605  2012-05-02 00:00:00.000 20:57:54

I am not sure how to go from where i am to where i need to be, so any help would be appreciated.

  • 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-17T03:13:16+00:00Added an answer on June 17, 2026 at 3:13 am

    Assuming you are using SQL Server 2005+ (because of the [] I see)

    ;WITH latestResult
    AS
    (
        SELECT Gully_ID, MaxDate, MaxTime,
                ROW_NUMBER() OVER (PARTITION BY Gully_ID
                                    ORDER BY MaxDate DESC, MaxTime DESC) RN
        FROM tableName
    )
    SELECT Gully_ID, MaxDate, MaxTime
    FROM latestResult
    WHERE RN = 1
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a query that will update the value of rgn_no and
I'm trying to create a sitemap that will automatically update. I've done something similiar
I'm trying create a query that will output a total number, as well as
I am trying to create a query that will allow me to split a
I'm trying to create a trigger that will update my Orders Table with the
I am trying to make a simple query that will update a user database
I'm trying to create an SQL query in PHP to update a table. Is
I am trying to create a trigger to update another table with a condition
I am trying to create a SQL script that will create a nice easy
I'm trying to create a web application that will allow a user to post

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.