List the number of rows for a field value in SQL

database / sql newbie here.

I have a table with a column called, for example, "EmployeeID". I want the query, for each individual employeeID, to return the number of rows that have this as their identifier. I hope that it is clear what I am trying to do, and someone will know how to help!

I do not think this should matter, but just in case I use MS SQL Server 2008.

+5
source share
5 answers

Plain SQL

select EmployeeId,count(*)
from YourTable
Group by EmployeeId
+12
source

Using:

  SELECT t.employeeid,
         COUNT(*) AS num_instances
    FROM TABLE t
GROUP BY t.employeeid

COUNT is an aggregate function that requires the use of a GROUP BY clause.

+4

:

SELECT employeeID, COUNT(employeeID) FROM Employees GROUP BY employeeID
+2
select count(*) AS RowCount, EmployeeID
FROM table
GROUP BY EmployeeID
+2
SELECT DISTINCT employeeID,
COUNT(employeeID) AS [Count]
FROM Employees
GROUP BY employeeID
-1

All Articles