views:

50

answers:

5

Hi, database/SQL novice here.

I have a table with a column called, for example, "EmployeeID". I want a query that will, for each distinct employeeID, return the number of rows that have that as their ID. I hope it's clear what I'm trying to do and someone will know how to help!

I don't think it should matter, but just in case, I'm using MS SQL Server 2008.

+1  A: 

This should do the trick:

SELECT employeeID, COUNT(employeeID) FROM Employees GROUP BY employeeID
Abe Miessler
+3  A: 

Simple SQL

select EmployeeId,count(*)
from YourTable
Group by EmployeeId
josephj1989
Perfect! I didn't know about the "group by" keyword. Thanks for the help.
Brennan Vincent
+3  A: 

Use:

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

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

OMG Ponies
+1  A: 
select count(*) AS RowCount, EmployeeID
FROM table
GROUP BY EmployeeID
Martin Smith
A: 
SELECT DISTINCT employeeID,
COUNT(employeeID) AS [Count]
FROM Employees
GROUP BY employeeID
flayto
Don't need DISTINCT when grouping by the value.
OMG Ponies