办公室总共12个人,分为6个小组,每天要有一组人打扫卫生。
我们的工作日是:周一到周五
给一个初始日期,算出本年度,这个小组值日日期!
 --构造数据
--构造数据 select identity(int,1,1) as iid,*
select identity(int,1,1) as iid,*  into #temp
into #temp from
from  (
( select top 100 percent [date]=dateadd(day,id,'2004-01-01'),[weekday]=datename(weekday,dateadd(day,id,'2004-01-01'))
select top 100 percent [date]=dateadd(day,id,'2004-01-01'),[weekday]=datename(weekday,dateadd(day,id,'2004-01-01')) from (
 from ( select id=a.id+b.id*10+c.id*100 from
select id=a.id+b.id*10+c.id*100 from  (
( select id=0 union all select 1
    select id=0 union all select 1 union all select id=2 union all select 3
    union all select id=2 union all select 3 union all select id=4 union all select 5
    union all select id=4 union all select 5 union all select id=6 union all select 7
    union all select id=6 union all select 7 union all select id=8 union all select 9
    union all select id=8 union all select 9 ) a,(
) a,( select id=0 union all select 1
    select id=0 union all select 1 union all select id=2 union all select 3
    union all select id=2 union all select 3 union all select id=4 union all select 5
    union all select id=4 union all select 5 union all select id=6 union all select 7
    union all select id=6 union all select 7 union all select id=8 union all select 9
    union all select id=8 union all select 9 ) b,(
) b,( select id=0 union all select 1
    select id=0 union all select 1 union all select id=2 union all select 3
    union all select id=2 union all select 3 ) c
) c ) aa
) aa where datepart(weekday,dateadd(day,id,'2004-01-01')) between 2 and 6
where datepart(weekday,dateadd(day,id,'2004-01-01')) between 2 and 6 and  id between 0 and 366
and  id between 0 and 366 order by id
order by id ) bb
) bb
 --查询
--查询 select [date]=convert(varchar(10),[date],120),[weekday] from #temp
select [date]=convert(varchar(10),[date],120),[weekday] from #temp where iid%6 = (select iid from #temp where [date] = '2004-11-16')%6
where iid%6 = (select iid from #temp where [date] = '2004-11-16')%6 
 --删除临时表
--删除临时表 drop table #temp
drop table #temp
    问题主要在这里:
where iid%6 = (select iid from #temp where [date] = '2004-11-16')%6
iid为插的序列号, iid被6除的余数只要等于特殊日期(2004-11-16,这天是我值日)被6除的余数相等即可。
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号