Awesome! Now with the DDL+DML you gave us we can provide a solution :-)
The following solution based on the assumption that each Cust_id as not more than 2 columns. If this is not exactly your case, then we can change it a bit to fit your needs, but this should be the approach for the solution
Please check this simple query without any subqueries
SELECT t0.Cust_id,t0.order_date, t0.order_number,
MyDif = ABS(DATEDIFF(DAY,
t0.order_date,
COALESCE(
LEAD(t0.order_date) OVER (partition by t0.Cust_id order by t0.order_number),
LAG(t0.order_date) OVER (partition by t0.Cust_id order by t0.order_number)
)
))
FROM [calisma3_2] t0
You can directly UPDATE the table from the above SELECT or simply forget about the extra column which is not seeded and only include duplicate data. Using the extra column or not is a question of how you use the data but in general you can get the value on-the-fly whenever is needed using LAG and LEAD functions
-- First we will create the clustered index which fits both queries
CREATE CLUSTERED INDEX Inx_Cust_id_order_date ON [calisma3_2](Cust_id,order_number);
GO
SET STATISTICS IO ON
GO
;With MyCTE as(
SELECT t0.Cust_id,t0.order_date, t0.order_number,
MyDif = ABS(DATEDIFF(DAY,
t0.order_date,
COALESCE(
LEAD(t0.order_date) OVER (partition by t0.Cust_id order by t0.order_number),
LAG(t0.order_date) OVER (partition by t0.Cust_id order by t0.order_number)
)
))
FROM [calisma3_2] t0
)
UPDATE [calisma3_2] SET fark = MyDif
FROM [calisma3_2] INNER JOIN MyCTE ON [calisma3_2].Cust_id = MyCTE.Cust_id and [calisma3_2].order_number= MyCTE.order_number
GO
/*
Table 'calisma3_2'. Scan count 2, logical reads 43, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.
*/
UPDATE t
SET fark = DATEDIFF (DAY,
(SELECT order_date FROM calisma3_2 WHERE Cust_Id = t.Cust_Id and order_number = 1 ),
(SELECT order_date FROM calisma3_2 WHERE Cust_Id = t.Cust_Id and order_number = 3 ))
from calisma3_2 t
GO
/*
Table 'calisma3_2'. Scan count 21, logical reads 62, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.
Table 'Worktable'. Scan count 1, logical reads 23, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.
*/
Did you noticed the 21 scans as a result of subqueries?!? You have 10 rows and for each row you execute 2 sub-queries which add another scan to the IO statistics + you have 1 main scan of the table 1+10+10 = 21 scans. This sample has 10 rows only! And what if your table has 1000 rows or a real case table with several millions rows?
Anyway, you should always test on your specific case :-)