问一个sql语句提取数据
数据如下
1 5
1 2
2 3
2 6
3 3
3 1
3 3
提取如下
1 7
2 9
3 7
如何实现效果统计?
------解决方案-------------------- Select
Col1,
SUM(Col2) As Col2
From
TableName
Group By
Col1
------解决方案--------------------create table table1(column1 int,column2 int)
go
insert table1 values(1,5)
insert table1 values(1,2)
insert table1 values(2,3)
insert table1 values(2,6)
insert table1 values(3,3)
insert table1 values(3,1)
insert table1 values(3,3)
go
select distinct column1,(select sum(column2) from table1 b where b.column1=a.column1) total
from table1 a
order by column1
------解决方案--------------------create table #(column1 int,column2 int)
go
insert # values(1,5)
insert # values(1,2)
insert # values(2,3)
insert # values(2,6)
insert # values(3,3)
insert # values(3,1)
insert # values(3,3)
select column1,sum(column2) from #
group by column1
column1
----------- -----------
1 7
2 9
3 7
(所影响的行数为 3 行)
------解决方案--------------------select c1 , sum(isnull(c2,0)) from tb group by c1
这种类型的方法最好