On my database I’ve the desk named
Rank Identify Time 1 John 110 2 David 120 3 Bob 121 4 Hans 124 5 Claus 126 6 Gustav 140
I’m within the particular person, who has the closest follower.
Within the above desk David is adopted by Bob with a distinction of 1.
Hans is adopted by Claus with a distinction with a distinction of two.
I would like a SQL-query, which returns this:
Identify Time-Distinction David 1 Hans 2 Bob 3 John 10 Claus 14
Sure, this may be accomplished by becoming a member of the desk
RankList_T to it self:
choose r1.rank,r1.title,r1.time, r2.rank,r2.title,r2.time, r2.time-r1.time as time_diff from GUEST.[RANK_LIST_T] r1 left be a part of GUEST.[RANK_LIST_T] r2 on r1.rank+1=r2.rank the place r2.rank isn't null order by r2.time-r1.time ;
However this has a really poor performane with an algorithm complexity of O(n^2).
I’m in search of a extra environment friendly means to do this on a SQL Database.
Please discover the instance for SQL Server right here: