[英]How to get the value associated with min of one column and the value associated with the max of the same column?
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 LEG_NUMBER | ORIGIN起源 | DESTINATION目的地 |
---|---|---|---|
ORD_200 ORD_200 | 1 1 | Utah犹他州 | California加利福尼亚 |
ORD_200 ORD_200 | 2 2 | California加利福尼亚 | New York纽约 |
ORD_200 ORD_200 | 3 3 | New York纽约 | Pennsylvania宾夕法尼亚州 |
Desired Output:期望的输出:
ORDER_NUMBER订单号 | ORIGIN起源 | DESTINATION目的地 |
---|---|---|
ORD_200 ORD_200 | Utah犹他州 | Pennsylvania宾夕法尼亚州 |
You can use a window function and distinct
您可以使用窗口函数和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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.