使用COUNT和GROUP BY进行查询

时间:2016-10-15 00:02:39

标签: sql-server count aggregate-functions

第一张表:员工

+------------+-----------+----------+-----+--------+--------+-------+
| EmployeeID | FirstName | LastName | Age | Gender | Salary | JobID |
+------------+-----------+----------+-----+--------+--------+-------+

第二张表: JobTitle

+-------+----------+----------------+--------+
| JobID | JobTitle | JobDescription | Status |
+-------+----------+----------------+--------+

编写一个SQL查询来统计所有员工和GROUP BY年龄和性别。 (输出显示别名)

我已完成以下查询:

SELECT COUNT(EmployeeID) AS Number_Of_Employees
FROM Employee
GROUP BY Age, Gender;

SELECT COUNT(*) AS 'Total_Employees'
FROM Employee
WHERE Gender = 'M' OR Gender= 'F'
GROUP BY Age, Gender;

这是两个查询的结果:

This is the result with both queries

1 个答案:

答案 0 :(得分:0)

我想我找到了答案。我最终运行了这个查询,我认为它有效。

SELECT Age, Gender, 
COUNT(EmployeeID) AS Total_Employee
FROM Employee
GROUP BY Age, Gender;