我有一张船运公司的桌子.
这些列是(NationalID、Shipping Date、ArraivalDate..)
问题是: 我如何才能获得每个用户的first shippment date and arrival date和last shippment date and arrival date
以下是示例数据:
NationalID | shippmetdate | arrivalDate |
---|---|---|
115 | 2020-10-08 | 2021-03-18 |
115 | 2023-02-08 | 1990-01-01 |
115 | 2021-08-11 | 2021-08-20 |
我使用此查询,但它不起作用
select NationalID,
min(shippmetdate) as first_shippment,
min(arrivalDate) as arrival_first,
max(shippmetdate) as last_shippment,
max(arrivalDate)as arrival_last
from (
select NationalID, shippmetdate, arrivalDate,
ROW_NUMBER() over (partition by NationalID order by shippmetdate) as seq1,
ROW_NUMBER() over (partition by NationalID order by arrivalDate) as seq2
from shippment
) t
group by NationalID
输出为:
NationalID | first_shippment | arrival_first | last_shippment | arrival_last |
---|---|---|---|---|
115 | 2020-10-08 | 1990-01-01 | 2023-02-08 | 2021-08-20 |
BUT I want the output like个
NationalID | first_shippment | arrival_first | last_shippment | arrival_last |
---|---|---|---|---|
115 | 2020-10-08 | 2021-03-18 | 2023-02-08 | 1990-01-01 |
Actually I want get the row of the first_shippment then I will return the arrival date for same row个
我try 使用max和min作为日期,但输出不匹配.