How to select only the earliest date per group

Mark van Luijtelaar 20 Reputation points
2023-04-12T11:49:19.1566667+00:00

I am looking for a way to select only the lines with the earliest date per order-operation line. So in this case let's say that the database is called orderlines and it looks like : User's image

I am looking for a way to select the earliest date per Orderopr line so per 123-10, 123-20, 124-10 etc. For some reaso I do not succeed in selecting only the earliest dates....

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,361 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.7K Reputation points
    2023-04-12T12:39:17.83+00:00

    Check one on typical solutions:

    select [order], operation, Orderopr, [date]
    from 
    (
       select *, row_number() over (partition by [order], operation order by [date]) n
       from orderlines
    ) o
    where n = 1
    order by [order], operation
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful