T
T
TroyashkA2014-10-13 11:32:30
SQL
TroyashkA, 2014-10-13 11:32:30

How to get one row having MAX(priority) in MSSQL?

Greetings.
There are signs "Customers" and "Orders". Communication by client ID.
The Orders table also contains the Order Date and Order Priority fields.
Task: Get the customer and, if it exists, their last order with the highest priority. The client must be received in any case, even if he has no orders.
ps it is assumed that the answer will be 1 line containing information about the client and his order

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Panov, 2014-10-13
@PanovAndrey

SELECT
  ClientSet.*
FROM ClientSet LEFT JOIN (SELECT
                          *
                          ,ROW_NUMBER() OVER (PARTITION BY Client_Id ORDER BY Priority ) as rn
                         FROM OrderSet) on OrderSet.Client_Id=ClientSet .ID
WHERE OrderSet.rn=1 OR OrderSet.rn IS NULL

Seems like it should work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question