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 |
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.