小二哥's blog

----zhangzs8896(小二)

导航

如何计算自己哪天打扫卫生

Posted on 2004-11-18 11:37  小二哥  阅读(787)  评论(2编辑  收藏  举报

办公室总共12个人,分为6个小组,每天要有一组人打扫卫生。
我们的工作日是:周一到周五
给一个初始日期,算出本年度,这个小组值日日期!

--构造数据
select identity(int,1,1as iid,* 
into #temp
from 
(
select top 100 percent [date]=dateadd(day,id,'2004-01-01'),[weekday]=datename(weekday,dateadd(day,id,'2004-01-01'))
 
from (
select id=a.id+b.id*10+c.id*100 from 
(
    
select id=0 union all select 1
    
union all select id=2 union all select 3
    
union all select id=4 union all select 5
    
union all select id=6 union all select 7
    
union all select id=8 union all select 9
) a,(
    
select id=0 union all select 1
    
union all select id=2 union all select 3
    
union all select id=4 union all select 5
    
union all select id=6 union all select 7
    
union all select id=8 union all select 9
) b,(
    
select id=0 union all select 1
    
union all select id=2 union all select 3
) c
) aa
where datepart(weekday,dateadd(day,id,'2004-01-01')) between 2 and 6
and  id between 0 and 366
order by id
) bb

--查询
select [date]=convert(varchar(10),[date],120),[weekday] from #temp
where iid%6 = (select iid from #temp where [date] = '2004-11-16')%6 

--删除临时表
drop table #temp

问题主要在这里:
where iid%6 = (select iid from #temp where [date] = '2004-11-16')%6
iid为插的序列号, iid被6除的余数只要等于特殊日期(2004-11-16,这天是我值日)被6除的余数相等即可。