reportid                             id23D6EC9C-FB74-46CE-A01A-1B777F4C2EDB              8
23D6EC9C-FB74-46CE-A01A-1B777F4C2EDB              7
23D6EC9C-FB74-46CE-A01A-1B777F4C2EDB              8
23D6EC9C-FB74-46CE-A01A-1B777F4C2EDB              6
CCBE35B5-29FA-45F9-BA8B-6993B495A813              6
CCBE35B5-29FA-45F9-BA8B-6993B495A813              7想要得到的结果是:
             reportid                             id23D6EC9C-FB74-46CE-A01A-1B777F4C2EDB             8 7 6
CCBE35B5-29FA-45F9-BA8B-6993B495A813             6 7

解决方案 »

  1.   

    将 一个  reportid 对应的几种状态 显示出来
      

  2.   

    create table tb (reportid nvarchar(100), id int)
    insert into tb values ('23D6EC9C-FB74-46CE-A01A-1B777F4C2EDB', 8)
    insert into tb values ('23D6EC9C-FB74-46CE-A01A-1B777F4C2EDB', 7)
    insert into tb values ('23D6EC9C-FB74-46CE-A01A-1B777F4C2EDB', 8)
    insert into tb values ('23D6EC9C-FB74-46CE-A01A-1B777F4C2EDB', 6)
    insert into tb values ('CCBE35B5-29FA-45F9-BA8B-6993B495A813', 6)
    insert into tb values ('CCBE35B5-29FA-45F9-BA8B-6993B495A813', 7)create FUNCTION dbo.f_str(@reportid nvarchar(100))
    RETURNS varchar(8000)
    AS
    BEGIN
      DECLARE @r varchar(8000)
      SELECT @r = isnull(@r + ' ','') + cast (id as varchar) FROM tb WHERE reportid=@reportid group by id
      RETURN @r
    END
    GO-- 调用函数
    SELECt reportid,  dbo.f_str(reportid) id FROM tb GROUP BY reportid