开发者

Specific Group By query in SQL Server

initial table:

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

after group by on third column and sum on that column:

 1   1   7
 1   2   4
 2开发者_StackOverflow社区   1   7
 2   2   5


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

Something like that??


Specify multiple columns to group by and it will only group when the values in all the columns are the same:

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


    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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜