列出SQL中每个字段值的行数

时间:2021-12-03 09:07:34

database/SQL novice here.

数据库/ SQL新手在这里。

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!

我有一个名为“EmployeeID”的列的表。我想要一个查询,对于每个不同的employeeID,它将返回具有该ID作为其ID的行数。我希望很清楚我正在尝试做什么,有人会知道如何提供帮助!

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

我认为这不重要,但为了以防万一,我正在使用MS SQL Server 2008。

5 个解决方案

#1


12  

Simple SQL

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

#2


4  

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.

COUNT是一个聚合函数,需要使用GROUP BY子句。

#3


2  

This should do the trick:

这应该是诀窍:

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

#4


2  

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

#5


-1  

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

#1


12  

Simple SQL

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

#2


4  

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.

COUNT是一个聚合函数,需要使用GROUP BY子句。

#3


2  

This should do the trick:

这应该是诀窍:

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

#4


2  

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

#5


-1  

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