I'm calculating a linear regression in SQL and need to preserve the Sort order in the #regression_data table.
I'm creating a time series regression and the row_number() is creating the count for X values. The regression needs to maintain the proper sort order to function properly.
The following code works properly when I don't insert into a temp table because I have the ability to sort. Even if I use don't create a Temp Table I don't believe I sort within a derived query.
if object_id('tempdb..#regression_data') is not null begin drop table #regression_data end
Select CAST(ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) as INT) 'x', CAST([Pax] as INT) 'y', *
--into #regression_data
from #TBB_Temp
´ Order by [Depart Year],[Booking Year], [Sorter]