-1

I have an original table with records of orders. How can go about getting an output where my result gives me the origin of the first leg and the destination of the final leg for the same order number?

ORDER_NUMBER LEG_NUMBER ORIGIN DESTINATION
ORD_200 1 Utah California
ORD_200 2 California New York
ORD_200 3 New York Pennsylvania

Desired Output:

ORDER_NUMBER ORIGIN DESTINATION
ORD_200 Utah Pennsylvania
JimLee123
  • 19
  • 4

1 Answers1

0

You can use a window function and distinct

select distinct ORDER_NUMBER
      , first_value(ORIGIN) over(partition by ORDER_NUMBER order by LEG_NUMBER ) ORIGIN
      , first_value(DESTINATION) over(partition by ORDER_NUMBER order by LEG_NUMBER desc) DESTINATION
from tbl
Serg
  • 22,285
  • 5
  • 21
  • 48