开发者

List number of rows per field value in SQL

开发者 https://www.devze.com 2023-01-04 10:13 出处:网络
database/SQL novice here. I have a table with a column called, for example, \"Emp开发者_Go百科loyeeID\". I want a query that will, for each distinct employeeID, return the number of rows that have th

database/SQL novice here.

I have a table with a column called, for example, "Emp开发者_Go百科loyeeID". 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.


Simple SQL

select EmployeeId,count(*)
from YourTable
Group by EmployeeId


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.


This should do the trick:

SELECT employeeID, COUNT(employeeID) FROM Employees GROUP BY employeeID


select count(*) AS RowCount, EmployeeID
FROM table
GROUP BY EmployeeID


SELECT DISTINCT employeeID,
COUNT(employeeID) AS [Count]
FROM Employees
GROUP BY employeeID
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号