可以用上一条记录的条件读出: update table set lastpoint = A.thispoint from table as A where 上一条记录这里关键是怎样确定上一条记录的条件: 1。可以是时间,最新的但不是这一条的时间进行判断; 2。用一个自增的键值,如ID INT IDENTITY(1,1),取最大的ID,但不是本条记录。
lastpoint字段是多余的。完全可以去掉。
同意 tj_dns(愉快的登山者 MVP) 观点。
1、用一个自增的键值,如ID INT IDENTITY(1,1)如id2、建立触发器:CREATE TRIGGER 名 on 表 INSTEAD OF INSERT AS BEGIN if exists(select 1 from 表) insert 表 (lastpoint,thispoint) select (select top 1 thispoint from 表 order by id desc),thispoint from inserted else insert 表 (lastpoint,thispoint) select thispoint,thispoint from inserted END 3、插入的时候: insert 表 (thispoint) values(22) insert 表 (thispoint) values(77)
可以用上一条记录的条件读出: update table set lastpoint = A.thispoint from table as A where 上一条记录这里关键是怎样确定上一条记录的条件: 1。可以是时间,最新的但不是这一条的时间进行判断; 2。用一个自增的键值,如ID INT IDENTITY(1,1),取最大的ID,但不是本条记录。
lastpoint字段是多余的,可以去掉。去掉之后报表的处理可以这样做: create table #temp (thispoint int) create table #temp2 (thispoint int,lastpoint int) insert into #temp values(5) insert into #temp values(6) insert into #temp values(8) insert into #temp values(3) declare @lastpoint int declare @thispoint int declare cur1 cursor for select * from #temp open cur1 fetch next from cur1 into @thispoint set @lastpoint=@thispoint while @@fetch_status=0 begin insert into #temp2 values(@thispoint,@lastpoint) set @lastpoint=@thispoint fetch next from cur1 into @thispoint end select * from #temp2 deallocate cur1 drop table #temp drop table #temp2
--下面是数据测试:--创建数据测试环境 declare @库存表a table(KehuId int,Date varchar(6),Count int) insert into @库存表A select 1,'200304',1000 union all select 1,'200305',2000 union all select 1,'200306',2500 union all select 2,'200304',3000 union all select 2,'200305',4000declare @库存表B table(KehuId int,Date varchar(6),LastCount int,NewCount int)--插入统计数量 insert into @库存表B(KehuId,Date,NewCount,LastCount) select KehuId,Date,Count ,(select Count from @库存表a where KehuId=a.KehuId and datediff(month,Date+'01',a.Date+'01')=1) from @库存表a a--显示处理结果 select * from @库存表B
update table set lastpoint = A.thispoint from table as A where 上一条记录这里关键是怎样确定上一条记录的条件:
1。可以是时间,最新的但不是这一条的时间进行判断;
2。用一个自增的键值,如ID INT IDENTITY(1,1),取最大的ID,但不是本条记录。
INSTEAD OF INSERT
AS
BEGIN
if exists(select 1 from 表)
insert 表 (lastpoint,thispoint) select (select top 1 thispoint from 表 order by id desc),thispoint from inserted
else
insert 表 (lastpoint,thispoint) select thispoint,thispoint from inserted
END 3、插入的时候:
insert 表 (thispoint) values(22)
insert 表 (thispoint) values(77)
update table set lastpoint = A.thispoint from table as A where 上一条记录这里关键是怎样确定上一条记录的条件:
1。可以是时间,最新的但不是这一条的时间进行判断;
2。用一个自增的键值,如ID INT IDENTITY(1,1),取最大的ID,但不是本条记录。
create table #temp (thispoint int)
create table #temp2 (thispoint int,lastpoint int)
insert into #temp values(5)
insert into #temp values(6)
insert into #temp values(8)
insert into #temp values(3)
declare @lastpoint int
declare @thispoint int
declare cur1 cursor for select * from #temp
open cur1
fetch next from cur1 into @thispoint
set @lastpoint=@thispoint
while @@fetch_status=0
begin
insert into #temp2 values(@thispoint,@lastpoint)
set @lastpoint=@thispoint
fetch next from cur1 into @thispoint
end
select * from #temp2
deallocate cur1
drop table #temp
drop table #temp2
http://expert.csdn.net/Expert/topic/2349/2349627.xml?temp=.436352
declare @库存表a table(KehuId int,Date varchar(6),Count int)
insert into @库存表A
select 1,'200304',1000
union all select 1,'200305',2000
union all select 1,'200306',2500
union all select 2,'200304',3000
union all select 2,'200305',4000declare @库存表B table(KehuId int,Date varchar(6),LastCount int,NewCount int)--插入统计数量
insert into @库存表B(KehuId,Date,NewCount,LastCount)
select KehuId,Date,Count
,(select Count from @库存表a where KehuId=a.KehuId and datediff(month,Date+'01',a.Date+'01')=1)
from @库存表a a--显示处理结果
select * from @库存表B