I want to create a rotating logic in sql like consider there are 3 numbers 1,2,3 then first week 1,2 will be selected next 3,1 next 2,3 and so on..... if there are 4 numbers 1,2,3,4 then 1,2 next 3,4 next 1,2 so on... Like that i want to generate the numbers in sql server.Please help me.
A:
You do like this:
declare @cnt int, @ofs int
select @cnt = count(*) from TheTable
set @ofs = (((@week - 1) * 2) % @cnt) + 1
select * from TheTable
where Number between @ofs and @ofs + 1
union all
select * from TheTable
where Number between @ofs - @cnt and @ofs - @cnt + 1
Guffa
2009-07-24 08:35:31
A:
Get the modulus of the number of weeks since your epoch date, then select all the entries from a numbers table that are between 1 and your limit except the modulus result.
eftpotrm
2010-03-12 14:38:50