I used the below query to retrieve the desired table:
with
PairingActivityInPairing AS (
SELECT DISTINCT
P.Id AS PairingId,
PD.Id AS PairingDutyId,
PA.Id AS PairingActivityId,
E.Id AS EmployeeId,
PEV.PairingEventTypeId,
PEV.StartDate AS PEStartDate,
FF.ArrivalActual AS FFArrival,
TZ.ZuluOffset
FROM
<my_table>
ORDER BY
P.Id, PD.Id, PA.Id
PairingId | PairingDutyId | PairingActivityId | EmployeeId | PairingEventTypeId | PEStartDate | FFArrival | ZuluOffset |
---|---|---|---|---|---|---|---|
2010 | 682 | 247907 | 10 | null | null | 2021-06-18 15:18:00 | null |
2010 | 682 | 247908 | 10 | null | null | 2021-06-18 20:15:00 | null |
2010 | 682 | 247909 | 10 | 8 | 2021-06-18 19:55:00 | null | 9.0 |
2010 | 683 | 247910 | 10 | null | null | 2021-06-20 01:55:00 | null |
1946 | 980 | 225388 | 3 | null | null | 2022-03-01 19:28:00 | null |
1946 | 980 | 225389 | 3 | 8 | 2023-03-01 19:40:00 | null | 9.0 |
1946 | 981 | 225390 | 3 | null | null | 2022-03-03 02:31:00 | null |
And I want to get the previous FFArrival of each row PARTITION BY PairingId, PairingDutyId
I tried to add the LAG() function in the query as below:
with
PairingActivityInPairing AS (
SELECT DISTINCT
P.Id AS PairingId,
PD.Id AS PairingDutyId,
PA.Id AS PairingActivityId,
E.Id AS EmployeeId,
PEV.PairingEventTypeId,
PEV.StartDate AS PEStartDate,
FF.ArrivalActual AS FFArrival,
LAG(FF.ArrivalActual) OVER (PARTITION BY P.Id, PD.Id ORDER BY PA.Id) AS LagFFArrival,
TZ.ZuluOffset
FROM
<my_table>
ORDER BY
P.Id, PD.Id, PA.Id
However, the queried result is not the same as expected one
PairingId | PairingDutyId | PairingActivityId | EmployeeId | PairingEventTypeId | PEStartDate | FFArrival | LagFFArrival | ZuluOffset |
---|---|---|---|---|---|---|---|---|
2010 | 682 | 247907 | 10 | null | null | 2021-06-18 15:18:00 | 2021-06-18 15:18:00 | null |
2010 | 682 | 247908 | 10 | null | null | 2021-06-18 20:15:00 | 2021-06-18 15:18:00 | null |
2010 | 682 | 247909 | 10 | 8 | 2021-06-18 19:55:00 | null | null | 9.0 |
2010 | 683 | 247910 | 10 | null | null | 2021-06-20 01:55:00 | 2021-06-20 01:55:00 | null |
1946 | 980 | 225388 | 3 | null | null | 2022-03-01 19:28:00 | 2022-03-01 19:28:00 | null |
1946 | 980 | 225389 | 3 | 8 | 2023-03-01 19:40:00 | null | null | 9.0 |
1946 | 981 | 225390 | 3 | null | null | 2022-03-03 02:31:00 | 2022-03-03 02:31:00 | null |
It simply copies the value of FFArrival and my expected result would be below
PairingId | PairingDutyId | PairingActivityId | EmployeeId | PairingEventTypeId | PEStartDate | FFArrival | LagFFArrival | ZuluOffset |
---|---|---|---|---|---|---|---|---|
2010 | 682 | 247907 | 10 | null | null | 2021-06-18 15:18:00 | null | null |
2010 | 682 | 247908 | 10 | null | null | 2021-06-18 20:15:00 | 2021-06-18 15:18:00 | null |
2010 | 682 | 247909 | 10 | 8 | 2021-06-18 19:55:00 | null | 2021-06-18 20:15:00 | 9.0 |
2010 | 683 | 247910 | 10 | null | null | 2021-06-20 01:55:00 | null | null |
1946 | 980 | 225388 | 3 | null | null | 2022-03-01 19:28:00 | null | null |
1946 | 980 | 225389 | 3 | 8 | 2023-03-01 19:40:00 | null | 2022-03-01 19:28:00 | 9.0 |
1946 | 981 | 225390 | 3 | null | null | 2022-03-03 02:31:00 | null | null |
Please help me to fix the issue. Thanks!
2条答案
按热度按时间sf6xfgos1#
fiddle
y0u0uwnf2#
If you include
LAG()
under theSELECT DISTINCT, DISTINCT
will mess up theORDER BY
sequence you put it withinLAG()
, making the final result non-ordered, which probably is not what you intend to do.It makes more sense to perform
LAG()
on the set of unique values afterDISTINCT
.DISTINCT
is an aggregate function that returns the set of unique values so it synthesizes/condenses information. Whereas window function (ie. LAG()) retains individual records.So