id name father_id
1  浙江   0
2  温州   1
3  瑞安   1
4  江西   0
5  九江   4现在我想建立一个视图,这个视图内容字段如下
第一个字段为所有非重复的father_id,
第二个字段为改father_id下 的所有id
father_id      son_id
0               1,4
1               2,3
4               5
可不知道创建该视图的sql语句怎么写。

解决方案 »

  1.   

    --带符号合并行列转换--有表t,其数据如下:
      a b
      1 1
      1 2
      1 3
      2 1
      2 2
      3 1
    --如何转换成如下结果:
      a b
      1 1,2,3
      2 1,2
      3 1 create table tb
    (
       a int,
       b int
    )
    insert into tb(a,b) values(1,1)
    insert into tb(a,b) values(1,2)
    insert into tb(a,b) values(1,3)
    insert into tb(a,b) values(2,1)
    insert into tb(a,b) values(2,2)
    insert into tb(a,b) values(3,1)
    goif object_id('pubs..f_hb') is not null
       drop function f_hb
    go--创建一个合并的函数
    create function f_hb(@a int)
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(b as varchar) from tb where a = @a 
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select distinct a ,dbo.f_hb(a) as b from tbdrop table tb--结果
    a           b     
    ----------- ------
    1           1,2,3
    2           1,2
    3           1(所影响的行数为 3 行)
    多个前列的合并
    数据的原始状态如下:
    ID  PR   CON  OP    SC 
    001 p    c    差    6
    001 p    c    好    2
    001 p    c    一般  4
    002 w    e    差    8
    002 w    e    好    7
    002 w    e    一般  1
    ===========================
    用SQL语句实现,变成如下的数据
    ID  PR   CON  OPS
    001 p    c    差(6),好(2),一般(4)
    002 w    e    差(8),好(7),一般(1)if object_id('pubs..tb') is not null
       drop table tb
    gocreate table tb
    (
    id varchar(10),
    pr varchar(10),
    con varchar(10),
    op varchar(10),
    sc int
    )
     
    insert into tb(ID,PR,CON,OP,SC) values('001', 'p',    'c',    '差',    6)
    insert into tb(ID,PR,CON,OP,SC) values('001', 'p',    'c',    '好',    2)
    insert into tb(ID,PR,CON,OP,SC) values('001', 'p',    'c',    '一般',  4)
    insert into tb(ID,PR,CON,OP,SC) values('002', 'w',    'e',    '差',    8)
    insert into tb(ID,PR,CON,OP,SC) values('002', 'w',    'e',    '好',    7)
    insert into tb(ID,PR,CON,OP,SC) values('002', 'w',    'e',    '一般',  1)
    goif object_id('pubs..test') is not null
       drop table test
    go
    select ID,PR,CON , OPS = op + '(' + cast(sc as varchar(10)) + ')' into test from tb--创建一个合并的函数
    if object_id('pubs..f_hb') is not null
       drop function f_hb
    go
    create function f_hb(@id varchar(10),@pr varchar(10),@con varchar(10))
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(OPS as varchar) from test where id = @id and @pr = pr and @con = con
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select distinct id ,pr , con , dbo.f_hb(id,pr,con) as OPS from testdrop table tb
    drop table test--结果
    id         pr         con        OPS                
    ---------- ---------- ---------- -------------------
    001        p          c          差(6),好(2),一般(4)
    002        w          e          差(8),好(7),一般(1)(所影响的行数为 2 行)create table b
    (col varchar(20))insert b values ('a')
    insert b values ('b')
    insert b values ('c')
    insert b values ('d')
    insert b values ('e')
    declare @sql varchar(1024)
    set @sql=''
    select @sql=@sql+b.col+',' from (select col from b) as b
    set @sql='select '''+@sql+''''
    exec(@sql)
      

  2.   

    if object_id('pubs..tb') is not null
       drop table tb
    gocreate table tb(id int,name varchar(10),father_id int)
    insert into tb(id,name,father_id) values(1,  '浙江',   0)
    insert into tb(id,name,father_id) values(2,  '温州',   1)
    insert into tb(id,name,father_id) values(3,  '瑞安',   1)
    insert into tb(id,name,father_id) values(4,  '江西',   0)
    insert into tb(id,name,father_id) values(5,  '九江',   4)
    go
    if object_id('pubs..f_hb') is not null
       drop function f_hb
    go--创建一个合并的函数
    create function f_hb(@father_id int)
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(id as varchar) from tb where father_id = @father_id
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select distinct father_id ,dbo.f_hb(father_id) as id from tbdrop table tb
    /*
    father_id   id 
    ----------- ---
    0           1,4
    1           2,3
    4           5(所影响的行数为 3 行)*/
      

  3.   

    谢谢楼上的,还有个疑问,请帮忙
    create function f_hb(@a int)
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(b as varchar) from tb where a = @a 
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go
    这里select @str = @str + ',' + cast(b as varchar) from tb where a = @a 
    如果@a在tb表的a字段找不到记录,我直接返回参数@a,请问高手该函数该怎么完善,
    第一次看sql函数。。汗。。帮忙