I finally found a solution based on my initial table, better performance than using CTE, and as said, https://stackoverflow.com/users/2522030/mike-abramczyk (a real table has 5k entries and using it offers lasted a long time).
After querying the lookup table, I added two rows to the table for each ProductID. These strings will receive a dummy StoreID (i.e. -9999): one with Min (DateID) - 1 and another with Max (DateID) + 1.
Insert into #Trackings (Aux_Row_Number,StoreID,DateID,ProductID,value) Select Min(Aux_Row_Number)-1 Aux_Row_Number,-9999 as StoreID, min(DateID)-1 as DateID,ProductID,Min(value) From #Trackings group by ProductID Order by ProductID Insert into #Trackings (Aux_Row_Number,StoreID,DateID,ProductID,value) Select Max(Aux_Row_Number)+1 Aux_Row_Number,-9999 as StoreID, max(DateID)+1 as DateID,ProductID,Max(value) From #Trackings group by ProductID Order by ProductID
Then I used the request you submitted to get the changes. Thus, I could get Change from Dummy (-9999) in real StoreID (1) and the last change from real StoreID (3) to Dummy (-9999).
select ISNULL(A.StoreID,-1) , ISNULL(B.StoreID,-1) , A.ProductID , A.value , B.value , A.DateID , B.DateID , ROW_NUMBER() OVER (Partition by B.ProductID Order by A.DateID) from
It was a decisive step! Now I can create a results table with the DateIDs of the changes I was looking for. The Aux_Row_Number column helped to get the sequence of changes for each product using (I think the last query created the #ProductStoreChanges table - I will post all Soltion below):
select A.ID_FinalStore,A.DateID_Final,B.DateID_Final,A.ProductID,B.value_2,A.value_2,B.value_2 - A.value_2 DeltaValue from
Here is the final solution:
IF OBJECT_ID('tempdb..#Trackings') Is Not Null Drop table #Trackings IF OBJECT_ID('tempdb..#ProductStoreChanges') Is Not Null Drop table #ProductStoreChanges Create Table #Trackings ( ProductID bigint , value float , StoreID int , DateID int , Aux_Row_Number int , flg_changed bit Default(0) ) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,1,1,325.2,1) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,1,2,326.2,2) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,1,3,329.6,3) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,2,4,335.2,4) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,2,5,336.5,5) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,2,6,338.3,6) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,3,7,339.2,7) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,3,8,342.1,8) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,1,9,343.7,9) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,1,10,355.0,10) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,1,12,385.0,12) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,3,13,485.0,13) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,3,14,985.0,14) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,3,15,1585.0,15) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,3,16,3585.0,16) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,2,17,5585.0,17) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,2,18,6585.0,18) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,2,19,8585.0,19) Insert into #Trackings(ProductID,StoreID,DateID,value,Aux_Row_Number) Values (100,2,20,9585.0,20) Insert into #Trackings (Aux_Row_Number,StoreID,DateID,ProductID,value) Select Min(Aux_Row_Number)-1 Aux_Row_Number,-9999 as StoreID, min(DateID)-1 as DateID,ProductID,Min(value) From #Trackings group by ProductID Order by ProductID Insert into #Trackings (Aux_Row_Number,StoreID,DateID,ProductID,value) Select Max(Aux_Row_Number)+1 Aux_Row_Number,-9999 as StoreID, max(DateID)+1 as DateID,ProductID,Max(value) From #Trackings group by ProductID Order by ProductID CREATE TABLE #ProductStoreChanges ( ID_InitialStore INT , ID_FinalStore INT , ProductID INT , value_1 BIGINT , value_2 BIGINT , DateID_Initial BIGINT , DateID_Final BIGINT , Aux_Row_Number INT ) INSERT INTO #ProductStoreChanges ( ID_InitialStore , ID_FinalStore , ProductID , value_1 , value_2 , DateID_Initial , DateID_Final , Aux_Row_Number ) select ISNULL(A.StoreID,-1) , ISNULL(B.StoreID,-1) , A.ProductID , A.value , B.value , A.DateID , B.DateID , ROW_NUMBER() OVER (Partition by B.ProductID Order by A.DateID) from #Trackings A Join #Trackings B On A.ProductID = B.ProductID And A.Aux_Row_Number + 1 = B.Aux_Row_Number AND ISNULL(A.StoreID,0) <> ISNULL(B.StoreID ,0) select A.ID_FinalStore,A.DateID_Final,B.DateID_Final,A.ProductID,B.value_2,A.value_2,B.value_2 - A.value_2 DeltaValue from #ProductStoreChanges A Join #ProductStoreChanges B On (A.Aux_Row_Number + 1 = B.Aux_Row_Number) And A.ProductID = B.ProductID Order by A.DateID_Final