首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求教,一个查询语句的写法解决方案

2012-04-09 
求教,一个查询语句的写法表 aidtime1time21200300130040021001002300500求查询结果为idsum(time1)sum (tim

求教,一个查询语句的写法
表 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 行受影响) 

热点排行