Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count multiple columns in SQL Server?

Tags:

c#

sql

I have a table like this:

enter image description here

How do I count rows that have identical values in columns A,B,C,D ?

The column 'ID' will be ignored.

For this case, the count result is 2.

like image 863
CCC Avatar asked Sep 08 '11 09:09

CCC


1 Answers

How about:

 SELECT COUNT(*), A, B, C, D
 FROM dbo.YourTable
 GROUP BY A, B, C, D
 -- optional - if you want to skip all the rows that occur only once
 -- HAVING COUNT(*) > 1 

Basically, you just group your data by the columns of interest, and let SQL count the rows that match each set of column values.

like image 176
marc_s Avatar answered Nov 07 '22 11:11

marc_s