I am trying to count the number of days between two dates that are present in the same column! when the ID is are same.
Here is a sample below…
ID Date
a 01/12/2024
a 01/13/2024
b 01/12/2024
b 01/20/2024
b 01/25/2024
c 01/12/2024
c 01/12/2024
Counted Days should appear in new column.
ID Date Days
a 01/12/2024 0
a 01/13/2024 1
b 01/12/2024 0
b 01/20/2024 8
b 01/25/2024 5
c 01/12/2024 0
c 01/12/2024 0
Any help is much appreciated (y).
2
Answers
You can do it using window function
LAG()
to retrieve previous rows andDATEDIFF
function to calculate the difference in days between two dates:Demo here
Does this example help?