select identity(int,1,0) as id ,b into # from tabe insert into b select id,sum(b) from # group by iddrop table #
create table A(B int) insert into A values(1) insert into A values(5) insert into A values(-3) insert into A values(2) insert into A values(5) insert into A values(3) insert into A values(-2) insert into A values(-8) go select B , id = identity(int,0,1) into tmp from Aselect id/4 , 结果 = sum(B) from tmp group by id/4drop table A,tmp/* 结果 ----------- ----------- 0 5 1 -2(所影响的行数为 2 行)*/
create table A(B int) insert into A values(1) insert into A values(5) insert into A values(-3) insert into A values(2) insert into A values(5) insert into A values(3) insert into A values(-2) insert into A values(-8) create table B(B int) goselect B , id = identity(int,0,1) into tmp from Ainsert into B select 结果 from (select id = id/4 , 结果 = sum(B) from tmp group by id/4) tselect * from Bdrop table A,B,tmp/* B ----------- 5 -2(所影响的行数为 2 行) */
declare @a table(a int) insert @a select 1 union all select 5 union all select -3 union all select 2 union all select 5 union all select 3 union all select -2 union all select -8 declare @b table(id int identity(0,1),a int) insert @b select * from @aselect sum(a) from @b group by id/4 --result /* ----------- 5 -2(所影响的行数为 2 行)*/
不好意思 ,我把函数参数搞错 select identity(int,0,1) as id ,b into # from tabe
select identity(int,1,0) as id ,b into # from table insert into b select sum(b) from # group by id/4 drop table #
create table #a (b int) insert into #a values(1) insert into #a values(5) insert into #a values(-3) insert into #a values(2) insert into #a values(5) insert into #a values(3) insert into #a values(-2) insert into #a values(-8)create table #b(b int)select id=identity(int,0,1) ,b into # from #ainsert into #b select b=sum(b) from # group by id/4 select * from #b b ----------- 5 -2(2 行受影响)
insert into b
select id,sum(b)
from #
group by iddrop table #
insert into A values(1)
insert into A values(5)
insert into A values(-3)
insert into A values(2)
insert into A values(5)
insert into A values(3)
insert into A values(-2)
insert into A values(-8)
go
select B , id = identity(int,0,1) into tmp from Aselect id/4 , 结果 = sum(B) from tmp group by id/4drop table A,tmp/*
结果
----------- -----------
0 5
1 -2(所影响的行数为 2 行)*/
insert into A values(1)
insert into A values(5)
insert into A values(-3)
insert into A values(2)
insert into A values(5)
insert into A values(3)
insert into A values(-2)
insert into A values(-8)
create table B(B int)
goselect B , id = identity(int,0,1) into tmp from Ainsert into B select 结果 from (select id = id/4 , 结果 = sum(B) from tmp group by id/4) tselect * from Bdrop table A,B,tmp/*
B
-----------
5
-2(所影响的行数为 2 行)
*/
insert @a select 1
union all select 5
union all select -3
union all select 2
union all select 5
union all select 3
union all select -2
union all select -8 declare @b table(id int identity(0,1),a int)
insert @b select * from @aselect sum(a) from @b group by id/4
--result
/*
-----------
5
-2(所影响的行数为 2 行)*/
select identity(int,0,1) as id ,b into # from tabe
insert into b
select sum(b)
from #
group by id/4
drop table #
create table #a (b int)
insert into #a values(1)
insert into #a values(5)
insert into #a values(-3)
insert into #a values(2)
insert into #a values(5)
insert into #a values(3)
insert into #a values(-2)
insert into #a values(-8)create table #b(b int)select id=identity(int,0,1) ,b into # from #ainsert into #b
select b=sum(b) from # group by id/4 select * from #b
b
-----------
5
-2(2 行受影响)