COUNT() 函数
你可以使用 COUNT()
函数返回与查询匹配的记录数。以下员工表包含员工 ID 号及其关联经理的 ID 号。
员工 ID | MANAGER_ID |
---|---|
12 |
37 |
22 |
37 |
37 |
63 |
42 |
45 |
45 |
63 |
57 |
45 |
59 |
45 |
63 |
COUNT()
语句可用于查明有多少员工拥有特定经理:
SELECT COUNT(*) AS CNT FROM Employees WHERE Employee.Manager_ID = 37;
返回
CNT |
---|
2
该函数也可以组合在更复杂的查询中。要了解有多少员工由指定人员直接监督,可以应用以下内容:
SELECT T1.Employee_ID,
(SELECT COUNT(*) AS CNT FROM Employees AS T2 WHERE T2.Manager_ID =
T1.Employee_ID) AS Supervised_Count
FROM Employees AS T1;
收益:
员工 ID | Supervised_Count |
---|---|
12 |
0 |
22 |
0 |
37 |
2 |
42 |
0 |
45 |
3 |
57 |
0 |
59 |
0 |
63 |
2 |
可以在此处找到 MSDN 文档。