单独的sql语句怕是很难实现,但是如果使用存储过程或是在前台做少量编码,都很容易实现。

解决方案 »

  1.   

    参考:http://expert.csdn.net/Expert/topic/2046/2046084.xml?temp=.8149835
      

  2.   

    不尽相同,
    我的要求是严格按照vPos的顺序取我所想要的数据
    flag 只有 1 , 2两个标识
      

  3.   

    create function getresult(@标识号 int)
    returns varchar(8000)
    as 
    begin
    declare @result varchar(8000)
    set @result=''
    select @result=@result+ch from 现有表 where flag=@标识号
    return @result
    end--语句:
    select distinct flag,dbo.getresult(flag) from 现有表
      

  4.   

    参考
    create table #tb(ch varchar(1),flag int,vpos int)
    insert into #tb
    select '1',1,1
    union all select '.',1,2
    union all select '0',1,3
    union all select '5',1,4
    union all select 'm',2,5
    union all select 'g',2,6
    union all select '3',1,8
    union all select '2',1,9
    union all select 'k',2,10
    union all select 'g',2,11select ch,flag,cast('' as varchar(8000)) as 值 into #temp from #tb
    declare @flag int,@str varchar(800)
    update #temp set @str=case when @flag=flag then @str+ch else ch end
    ,值=@str,@flag=flagselect * from #temp
    drop table #tb,#temp
      

  5.   

    create table #a (ch char(1),flag int,vpos int)
    insert #a values('1', 1 ,1)
    insert #a values('.', 1 ,2)
    insert #a values('0', 1 ,3)
    insert #a values('5', 1 ,4)
    insert #a values('m', 2 ,5)
    insert #a values('g', 2 ,6)
    insert #a values('3', 1 ,8)
    insert #a values('2', 1 ,9)
    insert #a values('k', 2 ,10)
    insert #a values('g', 2 ,11)declare @a varchar(8000),@b int,@c intselect *,0 gr,cast('' as varchar(8000)) value into #临时表 from #a
    set @c=0
    update #临时表 set @a=case when @b=flag then @a else '' end+ch,@c=case when @b=flag then @c else @c+1 end,@b=flag,gr=@c,value=@aselect max(value) result from #临时表 group by grgo
    drop table #临时表,#a
      

  6.   

    create table tb(ch varchar(1),flag int,vpos int)
    insert into tb
    select '1',1,1
    union all select '.',1,2
    union all select '0',1,3
    union all select '5',1,4
    union all select 'm',2,5
    union all select 'g',2,6
    union all select '3',1,8
    union all select '2',1,9
    union all select 'k',2,10
    union all select 'g',2,11declare @strsql varchar(500)
    select @strsql=''
    select @strsql=@strsql+ch from tb order by vpos
    select @strsql
    不用游标性质的我搞不定啊,不过感觉这个语句很有趣啊
      

  7.   

    不用游标,用临时表可以搞定
    我的测试代码是直接在原数据表加上:value varchar(8000),groupid int
    这两个字段,如果你不修改原表,就将原表的 ch,flag字段生成一个临时表,并加上上面的两个字段,就可以用我的代码:--创建数据测试环境
    declare @tb table(ch varchar(1),flag int,pvos int
    ,value varchar(8000),groupid int)
    insert into @tb(ch,flag,pvos)
    select '1',1,1
    union all select '.',1,2
    union all select '0',1,3
    union all select '5',1,4
    union all select 'm',2,5
    union all select 'g',2,6
    union all select '3',1,7
    union all select '2',1,9
    union all select 'k',2,10
    union all select 'g',2,11--处理数据
    declare @flag int,@ch varchar(8000),@groupid int
    set @groupid=0
    update @tb set @ch=case @flag when flag then @ch+ch else ch end
    ,@groupid=case @flag when flag then @groupid else @groupid+1 end
    ,value=@ch,groupid=@groupid--显示结果 ,@flag=flag
    select result
    from(
    select max(value) as result,groupid from @tb group by groupid
    ) a
      

  8.   

    pengdali(大力 V2.0) :
    你的策略比较好,早先我也想到将flag依照变化来修改成
    1,2,3,4来区别分段,但由于快下班了,所以没去实施,非常感谢
    大力,update #临时表 set @a=case when @b=flag then @a else '' end+ch,@c=case when @b=flag then @c else @c+1 end,@b=flag,gr=@c,value=@a
    这种方法我还是第一次用,最终我的方案是create table #a (ch char(1),flag int,vpos int)
    insert #a values('1', 1 ,1)
    insert #a values('.', 1 ,2)
    insert #a values('0', 1 ,3)
    insert #a values('5', 1 ,4)
    insert #a values('m', 2 ,5)
    insert #a values('g', 2 ,6)
    insert #a values('3', 1 ,8)
    insert #a values('2', 1 ,9)
    insert #a values('k', 2 ,10)
    insert #a values('g', 2 ,11)declare @d varchar(100) , @a varchar(100) , @b int,@c int
    set @a = ''
    set @d = ''
    select @d = @d + case when @b=flag then '' else @a end , @a=case when @b = flag then @a else '@' end + ch  , @b = Flag from #a
    select @d + @a
    结果: @1.05@mg@32@kg