Risultato finale:
| customer_id | gap_days |
|-------------|----------|
| 1 | 123 |
| 2 | 120 |
con cte come(
select order_id, customer_id, order_dt,
lag(order_dt) over(partition by customer_id order by order_dt) as prev_order_ft,
datediff(day, prev_order_ft, order_dt) as gap_days
da Orders
)
selezionare customer_id, max(gap_days) come gap_days
da cte
dove gap_days > 60
gruppo per customer_id