把原表T1的数据导入到新表T2中,其中T2比T1多一字段
T1中字段:井号jh,日期rq,发现时间time,异常代码nm,井段顶深aa
T2中字段:井号jh,日期rq,发现时间time,异常代码nm,井段顶深aa,序号xh
其中序号xh字段的值是根据T1中,井号和日期相同的情况下,第一条记录序号为1,发现时间,异常代码,井段顶深只要有一个与前条记录有变化则序号加1,这样应该如何写呢

解决方案 »

  1.   

    select t.井号jh,t.日期 rq,t.发现时间 time,t.异常代码 nm,t.井段顶深 
    aa, rank() over(partition by t.井号,t.日期 order by t.发现时间 ,t.异常代码,t.井段顶深) as 序号
    from T1 t
      

  2.   

    我的办法比较麻烦....
    声明两个type.type1,type2分别代表T1,T2
    声明h
    for item in(select  jh,rq,time,nm,aa order by jh,rq
    )-----------这里直接排下序...前提是...时间肯定不同....
    把item  值都放到  type1里....
     for i=1...type1的count  loop
      if i=1 then 
        h=1
      end if
     if i>1 then 
       if type1(i).jh=type1(i-1).jh and type1(i).rq=type1(i-1).rq then 
        h:=h+1
    end if 
      
      
     if type1(i).jh<>type1(i-1).jh or type1(i).rq<>type1(i-1).rq then 
       h :=1
    end if 
    end if   type2(i).xh:=h;
      type2(i).jh:=type1(i).jh;
     .
     .
     .
     .end loopinsert T2 ........