create table t
(id1 int ,id2 int ,id3 char(10))insert into t
select 1,2,1
union
select 1,2,2
union
select 1,3,1
union
select 1,3,2
union
select 1,4,2
create function f_str1(@id1 int,@id2 int)
returns varchar(1000)
as
begin
    declare @r varchar(1000)
    set @r = ''
    select @r = @r+','+rtrim(id3) from t where id1=@id1 and id2=@id2
    return(stuff(@r,1,1,''))
endselect id1,id2,dbo.f_str1(id1,id2) from t group by id1,id2

解决方案 »

  1.   

    select id1, id2, min(id3) as id3, count(id3) as ct from #t group by id1, id2
      

  2.   

    select d.mm+' '+c.id3 as fld ,d.counts from (
    select * from #t a where not exists 
    (select * from #t b where a.id1=b.id1 
    and a.id2=b.id2  and b.id3<a.id3))
    c inner join (
    select (cast(id1 as varchar(10))+' '+cast(id2 as varchar(10))) as mm 
     ,count(*) as counts from  #t  group by  (cast(id1 as varchar(10))+' '+cast(id2 as varchar(10)))) d
     on (cast(c.id1 as varchar(10))+' '+cast(c.id2 as varchar(10)))=d.mm
      

  3.   

    后面的那个不是用count出来的他可能是个字符啊