CREATE TABLE SheBeiRunHuaBiao --润滑设备表 ( F_NAME VARCHAR(50) --润滑名称 ,F_DATE DATETIME --润滑日期 ,F_CYCLE INT --润滑周期 ,F_MARK VARCHAR(10) --润滑标记,已完成,未完成 ) --基础数据 INSERT INTO SheBeiRunHuaBiao(F_NAME,F_DATE,F_CYCLE,F_MARK) --设备A SELECT '设备A','2013-09-01',2,'已完成' UNION ALL SELECT '设备A','2013-09-03',2,'已完成' UNION ALL SELECT '设备A','2013-09-05',2,'已完成' UNION ALL SELECT '设备A','2013-09-07',2,'已完成' UNION ALL SELECT '设备A','2013-09-09',2,'已完成' UNION ALL SELECT '设备A','2013-09-11',2,'已完成' UNION ALL SELECT '设备A','2013-09-13',2,'已完成' UNION ALL SELECT '设备A','2013-09-15',2,'已完成'
--设备B SELECT '设备B','2013-09-01',3,'已完成' UNION ALL SELECT '设备B','2013-09-04',3,'已完成' UNION ALL SELECT '设备B','2013-09-07',3,'已完成' UNION ALL SELECT '设备B','2013-09-10',3,'已完成' UNION ALL SELECT '设备B','2013-09-13',3,'已完成'
--设备C SELECT '设备C','2013-09-01',4,'已完成' UNION ALL SELECT '设备C','2013-09-05',4,'已完成' UNION ALL SELECT '设备C','2013-09-09',4,'已完成' UNION ALL SELECT '设备C','2013-09-13',4,'已完成'
--设备D SELECT '设备D','2013-09-01',5,'已完成' UNION ALL SELECT '设备D','2013-09-06',5,'已完成' UNION ALL SELECT '设备D','2013-09-11',5,'已完成'
INSERT INTO SheBeiRunHuaBiao(F_NAME,F_DATE,F_CYCLE,F_MARK) SELECT '设备A','2013-09-01',2,'已完成' UNION ALL SELECT '设备A','2013-09-03',2,'已完成' UNION ALL SELECT '设备A','2013-09-05',2,'已完成' UNION ALL SELECT '设备A','2013-09-07',2,'已完成' UNION ALL SELECT '设备A','2013-09-09',2,'已完成' UNION ALL SELECT '设备A','2013-09-11',2,'已完成' UNION ALL SELECT '设备A','2013-09-13',2,'已完成' UNION ALL SELECT '设备A','2013-09-15',2,'已完成' UNION ALL SELECT '设备B','2013-09-01',3,'已完成' UNION ALL SELECT '设备B','2013-09-04',3,'已完成' UNION ALL SELECT '设备B','2013-09-07',3,'已完成' UNION ALL SELECT '设备B','2013-09-10',3,'已完成' UNION ALL SELECT '设备B','2013-09-13',3,'已完成' UNION ALL SELECT '设备C','2013-09-01',4,'已完成' UNION ALL SELECT '设备C','2013-09-05',4,'已完成' UNION ALL SELECT '设备C','2013-09-09',4,'已完成' UNION ALL SELECT '设备C','2013-09-13',4,'已完成' UNION ALL SELECT '设备D','2013-09-01',5,'已完成' UNION ALL SELECT '设备D','2013-09-06',5,'已完成' UNION ALL SELECT '设备D','2013-09-11',5,'已完成'
-- 建存储过程 create proc sp_getSheBei
(@BeginDate varchar(20), @EndDate varchar(20)) as begin set nocount on create table #t(F_NAME varchar(10),F_DATE datetime,F_CYCLE int,F_MARK varchar(10))
insert into #t(F_NAME,F_DATE,F_CYCLE,F_MARK) select F_NAME,F_DATE,F_CYCLE,F_MARK from SheBeiRunHuaBiao where F_DATE between @BeginDate and @EndDate
while(@@rowcount>0) begin ;with t as (select F_NAME,F_DATE,F_CYCLE,F_MARK, row_number() over(partition by F_NAME order by F_DATE desc) 'rn' from #t) insert into #t(F_NAME,F_DATE,F_CYCLE,F_MARK) select a.F_NAME,dateadd(d,b.F_CYCLE,a.F_DATE),b.F_CYCLE,'未完成' from t a inner join t b on a.F_NAME=b.F_NAME and a.rn=1 and b.rn=1 where dateadd(d,b.F_CYCLE,a.F_DATE) between @BeginDate and @EndDate
end
select F_NAME,F_DATE,F_CYCLE,F_MARK from #t order by F_NAME,F_DATE end
INSERT INTO SheBeiRunHuaBiao(F_NAME,F_DATE,F_CYCLE,F_MARK) SELECT '设备A','2013-09-01',2,'已完成' UNION ALL SELECT '设备A','2013-09-03',2,'已完成' UNION ALL SELECT '设备A','2013-09-05',2,'已完成' UNION ALL SELECT '设备A','2013-09-07',2,'已完成' UNION ALL SELECT '设备A','2013-09-09',2,'已完成' UNION ALL SELECT '设备A','2013-09-11',2,'已完成' UNION ALL SELECT '设备A','2013-09-13',2,'已完成' UNION ALL SELECT '设备A','2013-09-15',2,'已完成' UNION ALL SELECT '设备B','2013-09-01',3,'已完成' UNION ALL SELECT '设备B','2013-09-04',3,'已完成' UNION ALL SELECT '设备B','2013-09-07',3,'已完成' UNION ALL SELECT '设备B','2013-09-10',3,'已完成' UNION ALL SELECT '设备B','2013-09-13',3,'已完成' UNION ALL SELECT '设备C','2013-09-01',4,'已完成' UNION ALL SELECT '设备C','2013-09-05',4,'已完成' UNION ALL SELECT '设备C','2013-09-09',4,'已完成'
UNION ALL SELECT '设备C','2013-09-13',4,'已完成' UNION ALL SELECT '设备D','2013-09-01',5,'已完成' UNION ALL SELECT '设备D','2013-09-06',5,'已完成' UNION ALL SELECT '设备D','2013-09-11',5,'已完成'
-- 建存储过程 create proc sp_getSheBei (@BeginDate varchar(20), @EndDate varchar(20)) as begin set nocount on
select distinct a.F_NAME, dateadd(d,a.F_CYCLE*b.number,a.F_DATE) 'F_DATE', a.F_CYCLE, case b.number when 0 then a.F_MARK else '未完成' end 'F_MARK' from (select F_NAME,F_DATE,F_CYCLE,F_MARK from SheBeiRunHuaBiao where F_DATE between @BeginDate and @EndDate) a cross join (select number from master.dbo.spt_values where type='P') b where dateadd(d,a.F_CYCLE*b.number,a.F_DATE) between @BeginDate and @EndDate and not exists (select 1 from SheBeiRunHuaBiao c where b.number>0 and c.F_DATE between @BeginDate and @EndDate and c.F_NAME=a.F_NAME and c.F_DATE=dateadd(d,a.F_CYCLE*b.number,a.F_DATE)) order by a.F_NAME,dateadd(d,a.F_CYCLE*b.number,a.F_DATE) end
INSERT INTO SheBeiRunHuaBiao(F_NAME,F_DATE,F_CYCLE,F_MARK) SELECT '设备A','2013-09-01',2,'已完成' UNION ALL SELECT '设备A','2013-09-03',2,'已完成' UNION ALL SELECT '设备A','2013-09-05',2,'已完成' UNION ALL SELECT '设备A','2013-09-07',2,'已完成' UNION ALL SELECT '设备A','2013-09-09',2,'已完成' UNION ALL SELECT '设备A','2013-09-11',2,'已完成' UNION ALL SELECT '设备A','2013-09-13',2,'已完成' UNION ALL SELECT '设备A','2013-09-15',2,'已完成' UNION ALL SELECT '设备B','2013-09-01',3,'已完成' UNION ALL SELECT '设备B','2013-09-04',3,'已完成' UNION ALL SELECT '设备B','2013-09-07',3,'已完成' UNION ALL SELECT '设备B','2013-09-10',3,'已完成'
UNION ALL SELECT '设备B','2013-09-13',3,'已完成' UNION ALL SELECT '设备C','2013-09-01',4,'已完成' UNION ALL SELECT '设备C','2013-09-05',4,'已完成' UNION ALL SELECT '设备C','2013-09-09',4,'已完成' UNION ALL SELECT '设备C','2013-09-13',4,'已完成' UNION ALL SELECT '设备D','2013-09-01',5,'已完成' UNION ALL SELECT '设备D','2013-09-06',5,'已完成' UNION ALL SELECT '设备D','2013-09-11',5,'已完成'
-- 建存储过程 create proc sp_getSheBei (@BeginDate varchar(20), @EndDate varchar(20)) as begin set nocount on
select distinct a.F_NAME, dateadd(d,a.F_CYCLE*b.number,a.F_DATE) 'F_DATE', a.F_CYCLE, case b.number when 0 then a.F_MARK else '未完成' end 'F_MARK' from (select F_NAME,F_DATE,F_CYCLE,F_MARK from SheBeiRunHuaBiao where F_DATE between @BeginDate and @EndDate union all select F_NAME,F_DATE,F_CYCLE,F_MARK from SheBeiRunHuaBiao e where not exists(select 1 from SheBeiRunHuaBiao f where f.F_NAME=e.F_NAME and f.F_DATE>e.F_DATE) and e.F_NAME not in(select distinct F_NAME from SheBeiRunHuaBiao where F_DATE between @BeginDate and @EndDate) ) a cross join (select number from master.dbo.spt_values where type='P') b where dateadd(d,a.F_CYCLE*b.number,a.F_DATE) between @BeginDate and @EndDate and not exists (select 1 from SheBeiRunHuaBiao c where b.number>0 and c.F_DATE between @BeginDate and @EndDate and c.F_NAME=a.F_NAME and c.F_DATE=dateadd(d,a.F_CYCLE*b.number,a.F_DATE)) order by a.F_NAME,dateadd(d,a.F_CYCLE*b.number,a.F_DATE) end