列出 SQL 中每个字段值的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3087020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List number of rows per field value in SQL
提问by Brennan Vincent
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”。我想要一个查询,该查询将为每个不同的员工 ID 返回以该 ID 作为 ID 的行数。我希望很清楚我要做什么,并且有人会知道如何提供帮助!
I don't think it should matter, but just in case, I'm using MS SQL Server 2008.
我认为这无关紧要,但以防万一,我使用的是 MS SQL Server 2008。
回答by josephj1989
Simple SQL
简单的 SQL
select EmployeeId,count(*)
from YourTable
Group by EmployeeId
回答by OMG Ponies
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 子句。
回答by Martin Smith
select count(*) AS RowCount, EmployeeID
FROM table
GROUP BY EmployeeID
回答by Abe Miessler
This should do the trick:
这应该可以解决问题:
SELECT employeeID, COUNT(employeeID) FROM Employees GROUP BY employeeID
回答by flayto
SELECT DISTINCT employeeID,
COUNT(employeeID) AS [Count]
FROM Employees
GROUP BY employeeID