datetime column

Phillip Noah 1 Reputation point
2021-11-15T14:39:45.717+00:00

Hi

How can i get the max datetime on my column for time of entry.

149443-image.png

Developer technologies Transact-SQL
{count} votes

6 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-11-15T14:52:05.75+00:00

    Try this query:

    select max(TimeOfEntry) as [max datetime] from MyTable
    
    0 comments No comments

  2. Phillip Noah 1 Reputation point
    2021-11-15T14:55:13.017+00:00

    Hi

    It doesn't work as it still returns the same values as the timeofentry


  3. Olaf Helper 47,436 Reputation points
    2021-11-15T15:27:53.59+00:00

    I expect the max datetime for each of the dates as the timeofentry

    Then aggregate on the date portion, like

    select cast(TimeOfEntry as date) as DateOfEntry, max(TimeOfEntry) as MaxTime
    from yourTable
    group by cast(TimeOfEntry as date)
    

  4. EchoLiu-MSFT 14,621 Reputation points
    2021-11-16T02:23:08.977+00:00

    Hi @Phillip Noah ,

    Welcome to the microsoft TSQL Q&A forum!

    Please also check:

    SELECT MAX(TimeOfEntry) OVER(PARTITION BY CAST(TimeOfEntry AS DATE)) AS maxdatetime  
    FROM yourTable  
    

    If you have any question, please feel free to let me know.

    Regards,
    Echo


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  5. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-11-17T22:02:09.843+00:00

    This is the Query i run below

    SELECT [StatusEntryID]
    ,cast(TimeOfEntry as date) as DateOfEntry, max(TimeOfEntry) as MaxTime
    ,[VehicleID]
    ,[Latitude]
    ,[Longitude]
    ,[NSIndicator]
    ,[EWIndicator]
    ,[SpeedKM]
    ,[Course]
    ,[Reserved]
    ,[StatusLogCalculated]
    FROM [Simecs].[dbo].[VehicleStatus]
    group by [StatusEntryID], cast(TimeOfEntry as date),[VehicleID]
    ,[Latitude]
    ,[Longitude]
    ,[NSIndicator]
    ,[EWIndicator]
    ,[SpeedKM]
    ,[Course]
    ,[Reserved]
    ,[StatusLogCalculated]

    That was not the query you were given. That query will give you the max query of every combination of Lat, Long, NSIndicator etc.

    desired output

    >

    Dateofentry maxtime VehicleID
    2016-02-03 2016-02-03 11:15:44 E1201
    2016-02-04 2016-02-03 11:25:18 E1201

    If you only want three columns, don't include other columns. Try:

    SELECT cast(TimeOfEntry as date) as DateOfEntry, max(TimeOfEntry) as MaxTime
    ,[VehicleID]
    FROM [
    group by cast(TimeOfEntry as date),[VehicleID]
    

    Or did you mean to say that you want the last row for the day for all vehicles? (I'm guessing wildly, as you are not doing a very good job of explaining what you want.) If so, try this:

    ; WITH CTE AS (
        SELECT [StatusEntryID]
          ,cast(TimeOfEntry as date) as DateOfEntry, TimeOfEntry
           ,[VehicleID]
           ,[Latitude]
           ,[Longitude]
           ,[NSIndicator]
           ,[EWIndicator]
           ,[SpeedKM]
           ,[Course]
           ,[Reserved]
           ,[StatusLogCalculated]
           , row_number() OVER (PARTITION BY VehicleID, cast(TimeOfEntry as date)
                                ORDER BY TimeEntry DESC) AS rowno
        FROM [Simecs].[dbo].[VehicleStatus]
    )
    SELECT [StatusEntryID], cast(TimeOfEntry as date),[VehicleID],
           TimeOfEntry
           ,[Latitude]
           ,[Longitude]
           ,[NSIndicator]
           ,[EWIndicator]
           ,[SpeedKM]
           ,[Course]
           ,[Reserved]
           ,[StatusLogCalculated]
    FROM   CTE
    WHERE rowno = 1
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.