I tried to get date difference in days from the same table column. For example customer 1234 first order date with the same date should be zero while the next diff orders (days) will be the difference in days between the first initial order date. Same will apply to customer 4321, his first initial order date will be 0 then the second will be the diff between the first and the third will be the diff between the second and the third.
select
a1.order_date,
(datediff(day, a2.order_date, a1.order_date)) new_value
from
#tmp a1
inner join
#tmp a2 on a1.customer_id = a2.customer_id
where
a1.customer_id = '1234'
and a1.TimeToPreviousOrder is null
and a1.stand is null
group by
a1.customer_id, a1.order_date, a2.order_date, a2.customer_id
customer_id | order_date | Diff in orders (days) |
---|---|---|
1234 | 2019-05-11 00:00:00.000 | 0 |
1234 | 2019-05-11 00:00:00.000 | 0 |
1234 | 2019-05-29 00:00:00.000 | 18 |
4321 | 2019-06-29 00:00:00.000 | 0 |
4321 | 2019-06-29 00:00:00.000 | 0 |
4321 | 2019-06-30 00:00:00.000 | 1 |