select top 1 * from tb union all select sum(A),sum(B),sum(C) from (select row_number() over(order by getdate()) no,* from tb) a where a.no>1
declare @tab table (A int,B int,C int) insert into @tab select 1,3,5 union all select 1,2,1 union all select 2,3,7create table #t (id int identity,A int,B int,C int) insert into #t select * from @tabselect A,B,C from #t where id=1 union all select sum(A),sum(B),sum(C) from #t where id<>1drop table #t /* A B C ----------- ----------- ----------- 1 3 5 3 5 8 */
select id=identity(int,1,1),* into #temp from tb ----弄个主键。 select A,B,C, Nid = (case when id = 1 then 0 else 1 end) into #temp2 from #temp select sum(A),sum(B),sum(C) from #temp2 group by Nid
--建表 create table #tab(A INT, B INT, C INT)insert into #tab select 1,3,5 union all select 1,2,1 union all select 2,3,7 --临时表 select identity(int, 1, 1) as autoid, * into #temp from #tab --结果 select a.a+isnull(b.a,0) as A,a.b+isnull(b.b,0) as B,a.c+isnull(b.c,0) as C from #temp as a left outer join #temp as b on a.autoid+1=b.autoid and a.autoid<>1 where a.autoid<>3 -- A B C ----------- ----------- ----------- 1 3 5 3 5 8(2 行受影响)
union all
select sum(A),sum(B),sum(C)
from (select row_number() over(order by getdate()) no,* from tb) a
where a.no>1
declare @tab table (A int,B int,C int)
insert into @tab
select 1,3,5 union all
select 1,2,1 union all
select 2,3,7create table #t (id int identity,A int,B int,C int)
insert into #t select * from @tabselect A,B,C from #t where id=1 union all
select sum(A),sum(B),sum(C) from #t where id<>1drop table #t
/*
A B C
----------- ----------- -----------
1 3 5
3 5 8
*/
select A,B,C, Nid = (case when id = 1 then 0 else 1 end) into #temp2 from #temp
select sum(A),sum(B),sum(C) from #temp2 group by Nid
create table #tab(A INT, B INT, C INT)insert into #tab
select 1,3,5
union all select 1,2,1
union all select 2,3,7
--临时表
select identity(int, 1, 1) as autoid, * into #temp from #tab
--结果
select a.a+isnull(b.a,0) as A,a.b+isnull(b.b,0) as B,a.c+isnull(b.c,0) as C
from #temp as a
left outer join #temp as b on a.autoid+1=b.autoid and a.autoid<>1
where a.autoid<>3
--
A B C
----------- ----------- -----------
1 3 5
3 5 8(2 行受影响)