同一个表里多条记录,把每条记录和和它一样NAME的前一条记录 合并成同一条记录 

    表 ID NAME     显示记录为
       ID            NAME        ID2              NAME2
     当前记录      nameXXX      前一条记录       也是nameXXX这样的SQL怎么写

解决方案 »

  1.   

    create table tb1
    (id int,name varchar(100))
    insert into tb1 select 1,'aaa'
    insert into tb1 select 2,'aaa'
    insert into tb1 select 3,'ccc'
    insert into tb1 select 4,'aaa'goselect identity(int,1,1) as idenid ,*  into  #temp from tb1goselect id,name,(select max(id) from #temp where name = a.name and idenid <a.idenid ) as id2,name as name2 from #temp a
    go
    /*1 aaa NULL aaa
    2 aaa 1 aaa
    3 ccc NULL ccc
    4 aaa 2 aaa*/drop table tb1,#temp
      

  2.   

    能不能只根据tb1
      来写SQL 不用这么复杂啊 还要用到临时表
      

  3.   

    select identity(int,1,1) as idenid ,*  into  #temp from tb1
    这个没明白 
    select id as idenid ,*  into  #temp from tb1
    这样行吗