sjhyf sZdbm sGX sJh sSJbysj sByfs
5 棉纺二厂   槽筒       25 2006-05-24 检修      
5 棉纺二厂   槽筒       26 2006-05-24 检修      
5 棉纺二厂   槽筒       27 2006-05-24 检修      
5 棉纺二厂   槽筒       3 2006-05-24 检修      
5 棉纺二厂   槽筒       9 2006-05-24 擦车      
5 棉纺二厂   槽筒       9 2006-05-24 检修      
如何将类似9号车的记录合并成以下方式
sjhyf sZdbm sGX sJh sSJbysj sByfs
5 棉纺二厂   槽筒       25 2006-05-24 检修      
5 棉纺二厂   槽筒       26 2006-05-24 检修      
5 棉纺二厂   槽筒       27 2006-05-24 检修      
5 棉纺二厂   槽筒       3 2006-05-24 检修      
5 棉纺二厂   槽筒       9 2006-05-24 擦车,检修       
  

解决方案 »

  1.   

    create table tb (sjhyf int,sZdbm varchar(10),sGX varchar(10),sJh int,sSJbysj varchar(10),sByfs varchar(10)) 
    insert into tb select  5,'棉纺二厂','槽筒',25,'2006-05-24','检修'      
    union all select 5,'棉纺二厂','槽筒',26,'2006-05-24','检修'      
    union all select 5,'棉纺二厂','槽筒',27,'2006-05-24','检修'      
    union all select 5,'棉纺二厂','槽筒',3,'2006-05-24','检修'      
    union all select 5,'棉纺二厂','槽筒',9,'2006-05-24','擦车'      
    union all select 5,'棉纺二厂','槽筒',9,'2006-05-24','检修'
    gocreate function dbo.fc_str(@sJh int)
    returns varchar(100)
    as
    begin
     declare @sql varchar(1000)
     set @sql=''
     select @sql=@sql+','+cast(sByfs as varchar) from tb where sJh=@sJh
     return stuff(@sql,1,1,'')
    end
    goselect sjhyf,sZdbm,sGX,sJh,sSJbysj,dbo.fc_str(sJh) as sByfs from tb group by sjhyf,sZdbm,sGX,sJh,sSJbysj order by sByfs desc
    drop table tb
    drop function dbo.fc_str