1: 计算值
2:可以不要 lastpoint 字段

解决方案 »

  1.   

    可以用上一条记录的条件读出:
    update table set lastpoint = A.thispoint from table as A where 上一条记录这里关键是怎样确定上一条记录的条件:
    1。可以是时间,最新的但不是这一条的时间进行判断;
    2。用一个自增的键值,如ID INT IDENTITY(1,1),取最大的ID,但不是本条记录。
      

  2.   

    lastpoint字段是多余的。完全可以去掉。
      

  3.   

    同意 tj_dns(愉快的登山者 MVP) 观点。
      

  4.   

    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)
      

  5.   

    可以用上一条记录的条件读出:
    update table set lastpoint = A.thispoint from table as A where 上一条记录这里关键是怎样确定上一条记录的条件:
    1。可以是时间,最新的但不是这一条的时间进行判断;
    2。用一个自增的键值,如ID INT IDENTITY(1,1),取最大的ID,但不是本条记录。
      

  6.   

    谢谢各位,我也觉得lastpoint字段多余,但报表时要用,如果数据库没有某个字段,可是报表里用怎么办
      

  7.   

    就像上面他们说的, 你可以定义一个时间字段或者id字段,然后排序后,每次循环读纪录的时候就保存这个thispoint,然后下一条记录就可以用上了阿, 至于第一条, 你首先把这个保存的变量的值定义为一个特殊的值比如该字段的最大值,然后循环的时候, 先判断是不是最大值, 是了, 就是第一条记录阿, 或者取得数据之后, 首先得到第一条纪录, 然后再开始循环检索数据
      

  8.   

    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
      

  9.   

    参考这个贴子,和你说的类似:
    http://expert.csdn.net/Expert/topic/2349/2349627.xml?temp=.436352
      

  10.   

    --下面是数据测试:--创建数据测试环境
    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
      

  11.   

    可以不要 lastpoint 字段,用存储过程建临时表