我现在有表measure(该表为一个器件计量检验表) ,结构如下:
leibie leixin xuhao jl_date period youxiao_date
B1 1 1 2005-11-5 4 2006-3-5
B1 1 2 2005-11-6 4 2006-3-6
....
B2 100 101 2005-7-5 6 2006-1-5
B3 1 1 2005-2-1 12 2006-2-1
我想根据measure表排出一个计划表,比如2006年的计划表,要求显示内容如下:
leibie leixin xuhao jl_date period youxiao_date
B1 1 1 2005-11-5 4 2006-3-5
B1 1 1 2006-3-5 4 2006-7-5
B1 1 1 2006-7-5 4 2006-11-5
B1 1 2 2006-11-6 4 2006-3-6
B1 1 2 2006-3-6 4 2006-7-6
B1 1 2 2006-7-6 4 2006-11-6
B2 100 101 2005-7-5 6 2006-1-5
B2 100 101 2006-1-5 6 2006-7-5
B3 100 101 2006-7-5 12 2006-2-1
备注:其中youxiao_date=jl_date+period ,比如我要排B1 1 1 2006年的检定计划就要有3月5日,7月5日,11月5日三条记录, 要根据jl_date 和 period推算出本年内的所有计划.
leibie leixin xuhao jl_date period youxiao_date
B1 1 1 2005-11-5 4 2006-3-5
B1 1 2 2005-11-6 4 2006-3-6
....
B2 100 101 2005-7-5 6 2006-1-5
B3 1 1 2005-2-1 12 2006-2-1
我想根据measure表排出一个计划表,比如2006年的计划表,要求显示内容如下:
leibie leixin xuhao jl_date period youxiao_date
B1 1 1 2005-11-5 4 2006-3-5
B1 1 1 2006-3-5 4 2006-7-5
B1 1 1 2006-7-5 4 2006-11-5
B1 1 2 2006-11-6 4 2006-3-6
B1 1 2 2006-3-6 4 2006-7-6
B1 1 2 2006-7-6 4 2006-11-6
B2 100 101 2005-7-5 6 2006-1-5
B2 100 101 2006-1-5 6 2006-7-5
B3 100 101 2006-7-5 12 2006-2-1
备注:其中youxiao_date=jl_date+period ,比如我要排B1 1 1 2006年的检定计划就要有3月5日,7月5日,11月5日三条记录, 要根据jl_date 和 period推算出本年内的所有计划.
到处都有你的身影啊
select * from measure
where year(youxiao_date)=2006
order by leibie,leixin,xuhao 如果youxiao_date不知道,DATEADD(mm, period,jl_date)来计算。
通过存储过程将记录写到新表中来实现吧
根据'2007-1-1'与jl_date月差除以period,就是需要添加的记录条数
insert into #1(eibie,leixin,xuhao,jl_date,period,youxiao_date)
select
'B1',1,1,'2005-11-5', 4,'2006-3-5'
union all select
'B1',1,2,'2005-11-6', 4,'2006-3-6'
union all select
'B2',100,101,'2005-7-5',6,'2006-1-5'
union all select
'B3',1,1,'2005-2-1',12,'2006-2-1'select *,num=0 into #2 from #1
where youxiao_date between '2006-01-01' and '2006-12-31'---这里假设表的数据唯一
declare @num int
set @num=1insert into #2
select eibie,leixin,xuhao,jl_date=youxiao_date,period,youxiao_date=dateadd(m,period,youxiao_date),1
from #2
where dateadd(m,period,youxiao_date) between '2006-01-01' and '2006-12-31'while (@@rowcount>0)
begin
set @num=@num+1 insert into #2
select eibie,leixin,xuhao,jl_date=youxiao_date,period,youxiao_date=dateadd(m,period,youxiao_date),@num
from #2
where num=@num-1 and dateadd(m,period,youxiao_date) between '2006-01-01' and '2006-12-31'
end
select eibie,leixin,xuhao,jl_date,period,youxiao_date from #2
order by eibie,leixin,xuhao,youxiao_date