--创建测试环境
create table test (id varchar(10),t1 int,t2 varchar(10),t3 varchar(10))
go
insert into test
select 'a1', 1, 'x1', 'aa'
union select 'a1' , 2,'b2','1b'
union select 'a2' , 3,'c3','3c'
union select 'a2' , 4,'q4','1d'
go
--测试
select id,sum(t1) t1
,(select top 1 t2 from test B where B.Id=A.ID) t2
,(select top 1 t3 from test B where B.Id=A.ID) t3
from test A group by id
--显示结果
id t1 t2 t3
a1 3 x1 aa
a2 7 c3 3c
--删除测试环境
drop table test
select * from
create table table1 (id varchar(10), t1 int, t2 varchar(10), t3 varchar(10))
go
insert into table1
select 'a1', 1, 'x1', 'aa'
union select 'a1' , 2,'b2','1b'
union select 'a2' , 3,'c3','3c'
union select 'a2' , 4,'q4','1d'
go
select * from table1
select id ,
sumt1 = (select sum(t1) from table1 c where id=b.id),
t2,t3
from table1 b
where t1=(select top 1 t1 from table1 where id=b.id)
drop table table1
go