rt
在已经排好顺序的表中,
将后面一行数据的其中一个字段,
放在该行数据前面那行的另一个字段中,
字段是字符串类型。
该表没有序列号之类的要怎样做呢??请给出详细sql语句。

解决方案 »

  1.   


    ---try
    select 字段=字段+(select substring(....) from TB where flag=g.flag-1) from(
    select flag=(select count(1) from TB where t.字段>=字段),* from TB T) g
      

  2.   

    --> 测试数据:@table
    declare @table table([Name] varchar(1),[PartyBranch] varchar(30),[TypeNo] varchar(30))
    insert @table
    select 'a','第一支部','正式党员' union all
    select 'b','第一支部','积极分子' union all
    select 'c','第一支部','正式党员' union all
    select 'd','第一支部','正式党员' union all
    select 'e','第一支部','正式党员' union all
    select 'f','第二支部','正式党员' union all
    select 'g','第二支部','正式党员' union all
    select 'h','第二支部','积极分子' union all
    select 'i','第二支部','正式党员'
    select identity(int,1,1) as id ,* into #temp from @tableselect name,PartyBranch + isnull((select TypeNo from #temp where id = t.id + 1),'') as PartyBranch,
    TypeNo
    from #temp tdrop table #temp
    --结果
    ----------------------------
    a 第一支部积极分子 正式党员
    b 第一支部正式党员 积极分子
    c 第一支部正式党员 正式党员
    d 第一支部正式党员 正式党员
    e 第一支部正式党员 正式党员
    f 第二支部正式党员 正式党员
    g 第二支部积极分子 正式党员
    h 第二支部正式党员 积极分子
    i 第二支部 正式党员
      

  3.   


    declare @tb table([id] int,[f1] varchar(3),[f2] varchar(4))
    insert @tb
    select 1,'aa','' union all
    select 2,'bb','' union all
    select 3,'bb1','' union all
    select 4,'bb2','' union all
    select 5,'bb3',''select a.id, a.f1, b.f1 as f2 
    from @tb a left join @tb b on a.id+2 = b.id--测试结果:
    /*id          f1   f2   
    ----------- ---- ---- 
    1           aa   bb1
    2           bb   bb2
    3           bb1  bb3
    4           bb2  NULL
    5           bb3  NULL(所影响的行数为 5 行)*/这样对么?
      

  4.   

    就拿3楼的数据来看吧
    name    PartyBranch     TypeNo
    a 第一支部积极分子 正式党员
    b 第一支部正式党员 积极分子
    c 第一支部正式党员 正式党员
    d 第一支部正式党员 正式党员
    e 第一支部正式党员 正式党员
    f 第二支部正式党员 正式党员
    g 第二支部积极分子 正式党员
    h 第二支部正式党员 积极分子
    i 第二支部 正式党员
    首先要加一列name_end,
    然后将每行数据的后一行的name字段的数据,放到前一行的name_end里面。
    就是这样。
    要做成如:
    name    PartyBranch     TypeNo  name_end
    a 第一支部积极分子 正式党员 b
    b 第一支部正式党员 积极分子 c
    c 第一支部正式党员 正式党员 d
    d 第一支部正式党员 正式党员 e
    e 第一支部正式党员 正式党员 f
    f 第二支部正式党员 正式党员 g
    g 第二支部积极分子 正式党员 h
    h 第二支部正式党员 积极分子 i
    i 第二支部 正式党员
    3楼写的什么啊,那不是建立了一个表吗。
      

  5.   

    --> 测试数据:@table
    declare @table table([Name] varchar(1),[PartyBranch] varchar(30),[TypeNo] varchar(30))
    insert @table
    select 'a','第一支部','正式党员' union all
    select 'b','第一支部','积极分子' union all
    select 'c','第一支部','正式党员' union all
    select 'd','第一支部','正式党员' union all
    select 'e','第一支部','正式党员' union all
    select 'f','第二支部','正式党员' union all
    select 'g','第二支部','正式党员' union all
    select 'h','第二支部','积极分子' union all
    select 'i','第二支部','正式党员'
    select identity(int,1,1) as id ,* into #temp from @tableselect name,PartyBranch ,TypeNo,
    name_end = (select name from #temp where id = t.id + 1)
    from #temp tdrop table #temp
    --结果
    -------------------------
    a 第一支部 正式党员 b
    b 第一支部 积极分子 c
    c 第一支部 正式党员 d
    d 第一支部 正式党员 e
    e 第一支部 正式党员 f
    f 第二支部 正式党员 g
    g 第二支部 正式党员 h
    h 第二支部 积极分子 i
    i 第二支部 正式党员 NULL
      

  6.   

    declare @table table([Name] varchar(1),[PartyBranch] varchar(30),[TypeNo] varchar(30))
    insert @table
    select 'a','第一支部','正式党员' union all
    select 'b','第一支部','积极分子' union all
    select 'c','第一支部','正式党员' union all
    select 'd','第一支部','正式党员' union all
    select 'e','第一支部','正式党员' union all
    select 'f','第二支部','正式党员' union all
    select 'g','第二支部','正式党员' union all
    select 'h','第二支部','积极分子' union all
    select 'i','第二支部','正式党员';with cte as
    (
    select rn=ROW_NUMBER()over(order by [name]),* from @table
    )
    select A.Name,a.PartyBranch,a.TypeNo,isnull(b.[name],'') as name_end
    from cte a left join cte b on a.rn=b.rn-1
    /*
    Name PartyBranch                    TypeNo                         name_end
    ---- ------------------------------ ------------------------------ --------
    a    第一支部                           正式党员                           b
    b    第一支部                           积极分子                           c
    c    第一支部                           正式党员                           d
    d    第一支部                           正式党员                           e
    e    第一支部                           正式党员                           f
    f    第二支部                           正式党员                           g
    g    第二支部                           正式党员                           h
    h    第二支部                           积极分子                           i
    i    第二支部                           正式党员                           */
      

  7.   


    if OBJECT_ID('tb') is not null drop table tb
    GO
    create table tb([Name] varchar(1),[PartyBranch] varchar(10),[TypeNo] varchar(10),TestCol varchar(10))
    GO
    insert tb
    select 'a','第一支部','正式党员','' union all
    select 'b','第一支部','积极分子','' union all
    select 'c','第一支部','正式党员','' union all
    select 'd','第一支部','正式党员','' union all
    select 'e','第一支部','正式党员','' union all
    select 'f','第二支部','正式党员','' union all
    select 'g','第二支部','正式党员','' union all
    select 'h','第二支部','积极分子','' union all
    select 'i','第二支部','正式党员',''GOwith cte as(select *,ROW_NUMBER()over(order by PartyBranch Desc )as Tag from tb)
    select a.*,Substring(c.TypeNo,1,2)
    from tb a left join cte b on a.Name=b.Name And a.PartyBranch=b.PartyBranch left join cte c on b.Tag=c.Tag-1-----------------------------------------
    Name PartyBranch TypeNo     TestCol    
    ---- ----------- ---------- ---------- ----
    a    第一支部        正式党员                  积极
    b    第一支部        积极分子                  正式
    c    第一支部        正式党员                  正式
    d    第一支部        正式党员                  正式
    e    第一支部        正式党员                  正式
    f    第二支部        正式党员                  正式
    g    第二支部        正式党员                  积极
    h    第二支部        积极分子                  正式
    i    第二支部        正式党员                  NULL(9 行受影响)