TSQL, counting pairs of values ​​in a table

Given a table in the format

  ID Forename Surname
 1 John Doe
 2 jane doe
 3 Bob Smith
 4 John Doe

How are you going to get the output

  Forename Surname Count
 John doe 2
 Jane doe 1
 Bob Smith 1

For a single column, I would just use count, but not sure how to apply this to multiple.

+6
sql tsql
source share
2 answers
SELECT Forename, Surname, COUNT(*) FROM YourTable GROUP BY Forename, Surname 
+9
source share

I think this should work:

 SELECT Forename, Surname, COUNT(1) AS Num FROM T GROUP BY Forename, Surname 
+1
source share

All Articles