-2

I have a purchase order table that has a list of order numbers and associated order lines with different dates. I want to a query to fetch the orders with orderlines that has the most recent date.

Order Table

I want the fetch to result in something like below

Result of my sql Query

7
  • 1
    There is only 1 order there though, Order1, so why are you expecting 3 rows and not 1 row?
    – Thom A
    Aug 6, 2020 at 16:54
  • 1
    Please update your question with the TSQL you've attempted to resolve your problem and explain the problem you're encountering with your query. Aug 6, 2020 at 16:56
  • 1
    Please avoid using images of tables, Aug 6, 2020 at 17:02
  • @GeorgeJoseph What is the better alternative?
    – Uuuuuumm
    Aug 6, 2020 at 17:04
  • 2
    Please do not post images of tables. Instead post the tables as ascii text in a code block, see Why should I provide a Minimal Reproducible Example for a very simple SQL query? for a tool to make ascii tables.
    – Scratte
    Aug 6, 2020 at 20:08

1 Answer 1

0

Here is a way to do this using row_number

select * from (
select t.*
       ,row_number() over(partition by ordernumber,orderlinenumber order by date desc) as rnk
  from <your_table> t
     )x
where x.rnk=1
1
  • That's excellent. Works just as i wanted it. Hope it does not give me any performance issues due to usage of row_number. Thanks George. Aug 6, 2020 at 17:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.