表A
master_id   type    desc
1            a1      cm
2            a2      mm
3            a1       m
4            a1      kg
5            a1      g
......表B
item_id    master_id    value
1            1            30
1            3            40
1            5            10
2            2            12
2            3            44
3            4            12
5            2            67
.....需要得出这样的结果item_id      value + desc
1            30cm,40m,10g,
2             44m
3             12kg
5             表A有5000+种   不要说用固定格式写....
表A只取type=a1的项目哪位高手能解答下  谢谢

解决方案 »

  1.   

    use Tempdb
    go
    --> --> 
     
    if not object_id(N'Tempdb..#A') is null
    drop table #A
    Go
    Create table #A([master_id] int,[type] nvarchar(2),[desc] nvarchar(2))
    Insert #A
    select 1,N'a1',N'cm' union all
    select 2,N'a2',N'mm' union all
    select 3,N'a1',N'm' union all
    select 4,N'a1',N'kg' union all
    select 5,N'a1',N'g'
    Go 
    if not object_id(N'Tempdb..#B') is null
    drop table #B
    Go
    Create table #B([item_id] int,[master_id] int,[value] int)
    Insert #B
    select 1,1,30 union all
    select 1,3,40 union all
    select 1,5,10 union all
    select 2,2,12 union all
    select 2,3,44 union all
    select 3,4,12 union all
    select 5,2,67
    Go
    WITH Cte
    AS
    (
    Select 
    b.[item_id],
    RTRIM(b.[value])+[desc] AS [value + desc]
    from #B AS b
    INNER JOIN #A AS c ON c.[master_id]=b.[master_id]
    )
    SELECT 
    [item_id],
    [value + desc]=STUFF((SELECT ','+[value + desc] FROM Cte WHERE item_id=a.[item_id] FOR XML PATH('')),1,1,'')
    FROM Cte AS a
    GROUP BY [item_id]
    /*
    item_id value + desc
    1 30cm,40m,10g
    2 12mm,44m
    3 12kg
    5 67mm*/
      

  2.   


    --> 数据库版本:
    --> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
    --> 测试数据:表A
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'表A') 
    AND type in (N'U')) 
    DROP TABLE 表A
    GO---->建表
    create table 表A([master_id] int,[type] varchar(2),[desc] varchar(2))
    insert 表A
    select 1,'a1','cm' union all
    select 2,'a2','mm' union all
    select 3,'a1','m' union all
    select 4,'a1','kg' union all
    select 5,'a1','g'
    GO--> 数据库版本:
    --> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
    --> 测试数据:表B
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'表B') 
    AND type in (N'U')) 
    DROP TABLE 表B
    GO---->建表
    create table 表B([item_id] int,[master_id] int,[value] int)
    insert 表B
    select 1,1,30 union all
    select 1,3,40 union all
    select 1,5,10 union all
    select 2,2,12 union all
    select 2,3,44 union all
    select 3,4,12 union all
    select 5,2,67
    GO
    ;with TB
    as
    (SELECT b.item_id,rtrim(b.value)+a.[desc]  as [value+desc] FROM 表A  a join 表B b  on 
    a.master_id=b.master_id
    WHERE type='a1')SELECT a.item_id,
    [value+desc] =stuff((
    select ','+convert(varchar(20),[value+desc]) FROM [TB] WHERE item_id = a.item_id  for xml path(''))
    ,1,1,'')
    FROM [TB] a 
    group by a.item_id
      

  3.   

    /*
    标题:按某字段合并字符串之一(简单合并)
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
    时间:2008-11-06
    地点:广东深圳描述:将如下形式的数据按id字段合并value字段。
    id    value
    ----- ------
    1     aa
    1     bb
    2     aaa
    2     bbb
    2     ccc
    需要得到结果:
    id     value
    ------ -----------
    1      aa,bb
    2      aaa,bbb,ccc
    即:group by id, 求 value 的和(字符串相加)
    */
    --1、sql2000中只能用自定义的函数解决
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    gocreate function dbo.f_str(@id varchar(10)) returns varchar(1000)
    as
    begin
      declare @str varchar(1000)
      select @str = isnull(@str + ',' , '') + cast(value as varchar) from tb where id = @id
      return @str
    end
    go--调用函数
    select id , value = dbo.f_str(id) from tb group by iddrop function dbo.f_str
    drop table tb
    --2、sql2005中的方法
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    goselect id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
    from tb
    group by iddrop table tb
    --3、使用游标合并数据
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    go
    declare @t table(id int,value varchar(100))--定义结果集表变量
    --定义游标并进行合并处理
    declare my_cursor cursor local for
    select id , value from tb
    declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
    open my_cursor
    fetch my_cursor into @id , @value
    select @id_old = @id , @s=''
    while @@FETCH_STATUS = 0
    begin
        if @id = @id_old
           select @s = @s + ',' + cast(@value as varchar)
        else
          begin
            insert @t values(@id_old , stuff(@s,1,1,''))
            select @s = ',' + cast(@value as varchar) , @id_old = @id
          end
        fetch my_cursor into @id , @value
    END
    insert @t values(@id_old , stuff(@s,1,1,''))
    close my_cursor
    deallocate my_cursorselect * from @t
    drop table tb
      

  4.   

    --drop table TTB
    --drop table TTA
    --drop table #a
    --drop table #b
    --建表
    Create table TTA
    (
      master_id  int,
      mtype varchar(20),
      mdesc  varchar(10)
    )
    Create table TTB
    (
      item_id  int,
      master_id  int,
      bvalue  int
    )
    insert into  TTA values(1,'a1','cm')
    insert into  TTA values(2,'a2','mm')
    insert into  TTA values(3,'a1','m')
    insert into  TTA values(4,'a1','kg')
    insert into  TTA values(5,'a1','g')insert into  TTB values(1,1,30)
    insert into  TTB values(1,3,40)
    insert into  TTB values(1,5,10)
    insert into  TTB values(2,2,12)
    insert into  TTB values(2,3,44)
    insert into  TTB values(3,4,12)
    insert into  TTB values(5,2,67)create table #a
    (
      itemid  int ,
      newvalues varchar(50)
    )
    create table #b
    (
      item_id  int ,
      new_values varchar(50)
    )----2初始结果
     insert into #a
     select item_id,cast(bvalue as varchar)+mdesc  as newvalues from TTB a left join 
       (
          select * from TTA where mtype='a1'
       ) b  
       on    a.master_id=b.master_idselect *  from #a
    --3最终结果
    declare @item_id int , @str varchar(1000)
    declare my_cursor cursor for
    select itemid  from #a
    open my_cursor
    fetch my_cursor into @item_id 
    while @@FETCH_STATUS = 0
    begin
      set @str=''
      SELECT @str = @str +cast(isnull(newvalues+',','') as varchar)+' '  from #a    where itemid=@item_id
      set @str=left(@str,len(@str)-1)
      insert into #b
      select @item_id,@str
      fetch my_cursor into @item_id
    END
    close my_cursor
    deallocate my_cursor
    select distinct *  from #b  
      

  5.   


    create table t1(master_id int identity(1,1),[type] varchar(3),[desc] varchar(10))create table t2(item_id int,master_id int,[value] int)insert t1([type],[desc]) select 'a1','cm' union all
    select 'a2','mm' union all
    select 'a1','m' union all
    select 'a1','kg'union all
    select 'a1','g'
    select * from t1
    insert t2 
    select 1, 1, 30 union all
    select 1, 3, 40 union all
    select 1, 5, 10 union all
    select 2, 2, 12 union all
    select 2, 3, 44 union all
    select 3, 4, 12 union all
    select 5, 2, 67
    select * from t2
    ;with cte as
    (select a.item_id,case b.[type]when 'a2' then null else (convert(varchar(10),a.[value])+b.[desc]) end as [value+desc] from t2 a left join t1 b on a.master_id=b.master_id 

    select a.item_id,stuff((select ','+b.[value+desc] from cte b where a.item_id=b.item_id for xml path('') ),1,1,'') as [value+desc] from cte a group by a.item_iditem      [value+desc]
    1   30cm,40m,10g
    2   44m
    3   12kg
    5   NULL