本帖最后由 loison 于 2014-04-16 10:11:04 编辑

解决方案 »

  1.   

    c4             p1                h1,h2,h3额, c4还掉了个h3
      

  2.   

    ----------------------------------------------------------------
    -- Author  :DBA_HuangZJ(发粪涂墙)
    -- Date    :2014-04-16 10:15:21
    -- Version:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
    -- Apr  2 2010 15:48:46 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
    --
    ----------------------------------------------------------------
    --> 测试数据[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([ID] int,[H] nvarchar(4),[P] nvarchar(4),[C] nvarchar(4))
    insert [huang]
    select 1,'h1','p1','c1' union all
    select 2,'h1','p1','c1' union all
    select 3,'h1','p1','c1' union all
    select 4,'h1','p1','c2' union all
    select 5,'h1','p1','c2' union all
    select 6,'h1','p1','c3' union all
    select 7,'h1','p1','c4' union all
    select 8,'h1','p1','c1' union all
    select 9,'h1','p2','c1' union all
    select 10,'h1','p2','c1' union all
    select 11,'h1','p2','c2' union all
    select 12,'h1','p3','c1' union all
    select 13,'h1','p3','c1' union all
    select 14,'h1','p1','c3' union all
    select 15,'h2','p1','c1' union all
    select 16,'h2','p1','c1' union all
    select 17,'h2','p1','c4' union all
    select 18,'h2','p1','c1' union all
    select 19,'h3','p1','c4' union all
    select 20,'h4','p1','c5'
    --------------生成数据--------------------------select a.[C],a.[P],
    stuff((select ','+[H] from huang b 
           where b.[C]=a.[C] and b.[P]=a.[P] 
           for xml path('')),1,1,'') 'H'
    from [huang] a
    group by  a.[C],a.[P]
    ----------------结果----------------------------
    /*
    C    P    H
    ---- ---- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    c1   p1   h1,h1,h1,h1,h2,h2,h2
    c1   p2   h1,h1
    c1   p3   h1,h1
    c2   p1   h1,h1
    c2   p2   h1
    c3   p1   h1,h1
    c4   p1   h1,h2,h3
    c5   p1   h4 
    */
      

  3.   

    2000和非2000的方法都有了:
    ----------------------------------------------------------------
    -- Author  :DBA_HuangZJ(发粪涂墙)
    -- Date    :2014-04-16 10:15:21
    -- Version:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
    -- Apr  2 2010 15:48:46 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
    --
    ----------------------------------------------------------------
    --> 测试数据[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([ID] int,[H] nvarchar(4),[P] nvarchar(4),[C] nvarchar(4))
    insert [huang]
    select 1,'h1','p1','c1' union all
    select 2,'h1','p1','c1' union all
    select 3,'h1','p1','c1' union all
    select 4,'h1','p1','c2' union all
    select 5,'h1','p1','c2' union all
    select 6,'h1','p1','c3' union all
    select 7,'h1','p1','c4' union all
    select 8,'h1','p1','c1' union all
    select 9,'h1','p2','c1' union all
    select 10,'h1','p2','c1' union all
    select 11,'h1','p2','c2' union all
    select 12,'h1','p3','c1' union all
    select 13,'h1','p3','c1' union all
    select 14,'h1','p1','c3' union all
    select 15,'h2','p1','c1' union all
    select 16,'h2','p1','c1' union all
    select 17,'h2','p1','c4' union all
    select 18,'h2','p1','c1' union all
    select 19,'h3','p1','c4' union all
    select 20,'h4','p1','c5'
    --------------生成数据--------------------------
    --2005写法
    select a.[C],a.[P],
    stuff((select ','+[H] from huang b 
           where b.[C]=a.[C] and b.[P]=a.[P] 
           for xml path('')),1,1,'') 'H'
    from [huang] a
    group by  a.[C],a.[P]--2000写法
    go
    if object_id('F_Str') is not null
        drop function F_Str
    go
    create function F_Str(@Col1 nvarchar(100),@col2 NVARCHAR(100))
    returns nvarchar(100)
    as
    begin
        declare @S nvarchar(100)
        select @S=isnull(@S+',','')+[H] from huang where [C]=@Col1 AND [P]=@col2
        return @S
    end
    go
    Select distinct [C],[P],H=dbo.F_Str([c],[p]) from huang
     
    ----------------结果----------------------------
    /*
    C    P    H
    ---- ---- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    c1   p1   h1,h1,h1,h1,h2,h2,h2
    c1   p2   h1,h1
    c1   p3   h1,h1
    c2   p1   h1,h1
    c2   p2   h1
    c3   p1   h1,h1
    c4   p1   h1,h2,h3
    c5   p1   h4(8 row(s) affected)C    P    H
    ---- ---- ----------------------------------------------------------------------------------------------------
    c1   p1   h1,h1,h1,h1,h2,h2,h2
    c1   p2   h1,h1
    c1   p3   h1,h1
    c2   p1   h1,h1
    c2   p2   h1
    c3   p1   h1,h1
    c4   p1   h1,h2,h3
    c5   p1   h4*/
      

  4.   

    额,H里面也有重复出现,P=p1的就行了
      

  5.   

    ----------------------------------------------------------------
    -- Author  :DBA_HuangZJ(发粪涂墙)
    -- Date    :2014-04-16 10:15:21
    -- Version:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
    -- Apr  2 2010 15:48:46 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
    --
    ----------------------------------------------------------------
    --> 测试数据[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([ID] int,[H] nvarchar(4),[P] nvarchar(4),[C] nvarchar(4))
    insert [huang]
    select 1,'h1','p1','c1' union all
    select 2,'h1','p1','c1' union all
    select 3,'h1','p1','c1' union all
    select 4,'h1','p1','c2' union all
    select 5,'h1','p1','c2' union all
    select 6,'h1','p1','c3' union all
    select 7,'h1','p1','c4' union all
    select 8,'h1','p1','c1' union all
    select 9,'h1','p2','c1' union all
    select 10,'h1','p2','c1' union all
    select 11,'h1','p2','c2' union all
    select 12,'h1','p3','c1' union all
    select 13,'h1','p3','c1' union all
    select 14,'h1','p1','c3' union all
    select 15,'h2','p1','c1' union all
    select 16,'h2','p1','c1' union all
    select 17,'h2','p1','c4' union all
    select 18,'h2','p1','c1' union all
    select 19,'h3','p1','c4' union all
    select 20,'h4','p1','c5'
    --------------生成数据--------------------------
    --2005写法
    select a.[C],a.[P],
    stuff((select ','+[H] from huang b 
           where b.[C]=a.[C] and b.[P]=a.[P] 
           for xml path('')),1,1,'') 'H'
    from [huang] a
    WHERE p='p1'
    group by  a.[C],a.[P]--2000写法
    go
    if object_id('F_Str') is not null
        drop function F_Str
    go
    create function F_Str(@Col1 nvarchar(100),@col2 NVARCHAR(100))
    returns nvarchar(100)
    as
    begin
        declare @S nvarchar(100)
        select @S=isnull(@S+',','')+[H] from huang where [C]=@Col1 AND [P]=@col2
        return @S
    end
    go
    Select distinct [C],[P],H=dbo.F_Str([c],[p]) from huang WHERE p='p1'
     
    ----------------结果----------------------------
    /*
    C    P    H
    ---- ---- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    c1   p1   h1,h1,h1,h1,h2,h2,h2
    c2   p1   h1,h1
    c3   p1   h1,h1
    c4   p1   h1,h2,h3
    c5   p1   h4(5 row(s) affected)C    P    H
    ---- ---- ----------------------------------------------------------------------------------------------------
    c1   p1   h1,h1,h1,h1,h2,h2,h2
    c2   p1   h1,h1
    c3   p1   h1,h1
    c4   p1   h1,h2,h3
    c5   p1   h4
    */
      

  6.   

    c1   p1   h1,h1,h1,h1,h2,h2,h2       这个不能为 h1,h2么重复这么多
    我sql2012
      

  7.   

    写错了,这个才对----------------------------------------------------------------
    -- Author  :DBA_HuangZJ(发粪涂墙)
    -- Date    :2014-04-16 10:15:21
    -- Version:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
    -- Apr  2 2010 15:48:46 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
    --
    ----------------------------------------------------------------
    --> 测试数据[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([ID] int,[H] nvarchar(4),[P] nvarchar(4),[C] nvarchar(4))
    insert [huang]
    select 1,'h1','p1','c1' union all
    select 2,'h1','p1','c1' union all
    select 3,'h1','p1','c1' union all
    select 4,'h1','p1','c2' union all
    select 5,'h1','p1','c2' union all
    select 6,'h1','p1','c3' union all
    select 7,'h1','p1','c4' union all
    select 8,'h1','p1','c1' union all
    select 9,'h1','p2','c1' union all
    select 10,'h1','p2','c1' union all
    select 11,'h1','p2','c2' union all
    select 12,'h1','p3','c1' union all
    select 13,'h1','p3','c1' union all
    select 14,'h1','p1','c3' union all
    select 15,'h2','p1','c1' union all
    select 16,'h2','p1','c1' union all
    select 17,'h2','p1','c4' union all
    select 18,'h2','p1','c1' union all
    select 19,'h3','p1','c4' union all
    select 20,'h4','p1','c5'
    --------------生成数据--------------------------
    --2005写法
    select a.[C],a.[P],
    stuff((select ','+[H] from (SELECT DISTINCT h,p,c  FROM  huang WHERE p='p1')  b 
           where b.[C]=a.[C] and b.[P]=a.[P] 
           for xml path('')),1,1,'') 'H'
    from (SELECT DISTINCT h,p,c FROM  huang WHERE p='p1') a
    group by  a.[C],a.[P]--2000写法
    go
    if object_id('F_Str') is not null
        drop function F_Str
    go
    create function F_Str(@Col1 nvarchar(100),@col2 NVARCHAR(100))
    returns nvarchar(100)
    as
    begin
        declare @S nvarchar(100)
        select @S=isnull(@S+',','')+[H] from (SELECT DISTINCT h,p,c  FROM  huang WHERE p='p1') huang where [C]=@Col1 AND [P]=@col2
        return @S
    end
    go
    Select distinct [C],[P],H=dbo.F_Str([c],[p]) FROM (SELECT DISTINCT h,p,c  FROM  huang WHERE p='p1') a
     
    ----------------结果----------------------------
    /*
    C    P    H
    ---- ---- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    c1   p1   h1,h2
    c2   p1   h1
    c3   p1   h1
    c4   p1   h1,h2,h3
    c5   p1   h4(5 row(s) affected)C    P    H
    ---- ---- ----------------------------------------------------------------------------------------------------
    c1   p1   h1,h2
    c2   p1   h1
    c3   p1   h1
    c4   p1   h1,h2,h3
    c5   p1   h4
    */