表 a
id time1 time2
1 200 300
1 300 400
2 100 100
2 300 500
求查询结果为
id sum(time1) sum (time2)
1 500 700
2 400 600
id为重复字段,要求查询出结果 每个id为条件 各个time1,time2值的和
------解决方案--------------------
- SQL code
--表 a--id time1 time2 --1 200 300--1 300 400--2 100 100--2 300 500 --求查询结果为 --id sum(time1) sum (time2)--1 500 700--2 400 600declare @a table(id int,time1 int,time2 int) insert into @a values(1 ,200 ,300),(1, 300 ,400),(2, 100 ,100),(2 ,300 ,500 ) select id ,(select SUM(time1) from @a where a.id=id)as 'sum(time1)',(select SUM(time2) from @a where a.id=id) as 'sum (time2)' from @a agroup by idid sum(time1) sum (time2)----------- ----------- -----------1 500 7002 400 600(2 行受影响)