I got values from distinct result and like to see the most recent date associated with it

Jonathan Brotto 420 Reputation points
2024-08-14T15:38:00+00:00

Hi,

I am trying to return the results for the first distinct values of a specific column, but also get an adjacent column such as date to see why these columns return a certain value.

User's image

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,492 questions
SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
155 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,693 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 73,001 Reputation points
    2024-08-14T20:36:01.2866667+00:00

    simple

    select strata, max(datecolumnname) as "date"
    group by strata
    order by strata
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 119.9K Reputation points MVP
    2024-08-14T19:40:16.0133333+00:00

    This sounds more like "what am I thinking on" game than an SQL question (because if it really had been an SQL question, you would have given use CREATE TABLE + INSERT with sample data and the expected results, hadn't you.)

    But, OK, I throw my had in the ring for the game and try with:

    ; WITH numbering AS (
         SELECT strata, date, row_number() OVER(PARTITION BY strata ORDER BY date DESC) AS rowno
    )SELECT strata
    FROM  numbering
    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.