2009-07-24 44 views
0

我想在sql中創建一個旋轉邏輯,就像考慮有3個數字1,2,3然後第一個星期1,2將選擇下一個3,1下一個2,3等....如果有4個數字1,2,3,4然後1,2下一個3,4下一個1,2等等...就像我想在sql server中生成數字。請幫助我。SQL旋轉數字

+1

我看不到您的示例中的常見模式。請再次檢查並提供您想要的完整列表(不要使用「等等」)。 – 2009-07-24 08:11:30

回答

1

你這樣做:

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 
0

獲取週數的模量,因爲你的紀元日期,然後選擇一個數字表爲1和除模結果你的極限之間的所有條目。