vinodacharyabva
vinodacharyabva

Reputation: 41

SQL Rotating numbers

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.

Upvotes: 0

Views: 334

Answers (2)

eftpotrm
eftpotrm

Reputation: 2271

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.

Upvotes: 0

Guffa
Guffa

Reputation: 700362

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

Upvotes: 1

Related Questions