Share via

select latest date

Steeves, Jason 1 Reputation point
2021-12-13T13:45:23.693+00:00

I have a table that has multiple date values and I want to select just the latest date
Table_Dates : Wkstn / Date_Created / Date_HWScan / Date_SWScan / Date_PatchScan / Date_CheckIn

So I'd like to have a query that will return Wkstn and a single date value that is the newest date.

Please advise if there's a way to complete this.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


4 answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,626 Reputation points
    2021-12-14T03:29:53.087+00:00

    Please also check:

        CREATE TABLE #Table_Dates  
        (Wkstn VARCHAR(MAX), Date_Created DATE,Date_HWScan   
        DATE,Date_SWScan DATE,Date_PatchScan DATE,Date_CheckIn DATE)  
          
        INSERT INTO #Table_Dates VALUES  
        ('Wkstn','2021-12-14','2021-12-21','2021-12-01','2021-11-30','2021-11-21')  
          
        SELECT Wkstn,MAX(dDate) maxDate    
        FROM #Table_Dates  
        UNPIVOT (dDate FOR nDate IN ( Date_Created,Date_HWScan   
        ,Date_SWScan ,Date_PatchScan,Date_CheckIn)) as u  
        GROUP BY Wkstn  
    

    Output:
    157260-image.png

    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.

    Was this answer helpful?

    0 comments No comments

  2. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2021-12-13T23:02:29.813+00:00

    Beside the answers in the thread that Nasreen pointed you to, here is a simpler solution, but which only works on Azure SQL Database and Azure SQL Managed Instance:

    SELECT Wkstn, greatest(Date_Created, Date_HWScan, Date_SWScan, Date_PatchScan, Date_CheckIn)
    FROM tbl
    

    Was this answer helpful?

    0 comments No comments

  3. Nasreen Akter 10,896 Reputation points Volunteer Moderator
    2021-12-13T14:24:22.973+00:00

    Hi @Steeves, Jason ,

    There is a similar thread, link is given below. Hope this will help. Thanks!

    https://stackoverflow.com/questions/71022/sql-max-of-multiple-columns

    Was this answer helpful?

    0 comments No comments

  4. Siva Kolusu 1 Reputation point
    2021-12-13T14:00:55.007+00:00

    @Jason Steeves ,Please try this.

    Select Wkstn,DateChkn --DateChkn = Replace Date Column based on requirement
    From Table Where DateChkn = (Select Max(DateChkn) From Table)

    Thanks.

    Was this answer helpful?

    0 comments No comments

Your answer

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