特定组按SQL Server中的查询

时间:2011-07-06 16:55:37

标签: sql-server-2008

初始表:

 1   1   3
 1   1   4
 1   2   1
 1   2   3
 2   1   5
 2   1   2
 2   2   2
 2   2   3

在第三列分组之后,对该列进行总结:

 1   1   7
 1   2   4
 2   1   7
 2   2   5

3 个答案:

答案 0 :(得分:5)

SELECT Col1, Col2, SUM(Col3)
FROM dbo.YourTable
GROUP BY Col1, Col2

东西  那个??

答案 1 :(得分:4)

指定要分组的多个列,并且仅在所有列中的值相同时才进行分组:

SELECT ColA, ColB, Sum(ColC) as Summation
FROM YourTable
GROUP BY ColA, ColB

答案 2 :(得分:0)

    select one , two, sum(three)  from (
  select 1 as one, 1 as two, 3 as three from dual
  union
  select 1 as one, 1 as two, 4 as three from dual
  union
  select 1 as one, 2 as two, 1 as three from dual
  union
  select 1 as one, 2 as two, 3 as three from dual
  union
  select 2 as one, 1 as two, 5 as three from dual
  union
  select 2 as one, 1 as two, 2 as three from dual
  union
  select 2 as one, 2 as two, 2 as three from dual
  union
  select 2 as one, 2 as two, 3 as three from dual
) group by one, two;