If r = number of rotations, a = int array, n = a.length, to rotate the array you need to
move a[r to n] to the beginning of the array
move a[0 to r] to end of array
Also r can be reduced to r % n, as for every n rotations, the array repeats.
int result = new int;
r = r % n;
int count = 0;
Oh well, I didnt notice the sql-server tag...
What a detailed execution plan they have :)
But anyway the answer may be as simple as that there is more answers than there are questions.
And because you are making another subselect for the column and you are selecting what you already have in the outer select, this may get expensive quite fast.
So I would ...