How to select only the earliest date per group

Mark van Luijtelaar 20 Reputation points

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.
12,884 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.9K Reputation points

    Check one on typical solutions:

    select [order], operation, Orderopr, [date]
       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