Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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   1   7
 2   2   5
like image 374
EMAI Avatar asked Apr 25 '26 03:04

EMAI


2 Answers

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

Something like that??

like image 128
marc_s Avatar answered Apr 27 '26 14:04

marc_s


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
like image 28
Lucent Fox Avatar answered Apr 27 '26 13:04

Lucent Fox