0
我有2表:如何在SQL中应用以下情况下的条件聚合?
- tbDepatrment 2. tbEmployee
结果所需要的:
Department Employees(by department) Male(By department) Female(by department)
Employee 11 6 5
Management 10 5 5
Owner 1 1 0
我用这个:
SELECT [tbDepartment].[DepartmentName] AS [Department name],
Count(*) AS [Number of employees by department type],
(Select Count([EmployeeGender]) FROM [tbEmployee] WHERE [EmployeeGender] = 'Male' AND [DepartmentName] = [tbDepartment].[DepartmentName]) AS [Number of male employees by department type],
(Select Count([EmployeeGender]) FROM [tbEmployee] WHERE [EmployeeGender] = 'Female' AND [DepartmentName] = [tbDepartment].[DepartmentName]) AS [Number of female employees by department type]
FROM [tbEmployee] JOIN [tbDepartment]
ON [tbEmployee].[DepartmentId] = [tbDepartment].[DepartmentId]
GROUP BY [tbDepartment].[DepartmentName];
导致为:
Department Employees(by department) Male(By department) Female(by department)
Employee 11 12 10
Management 10 12 10
Owner 1 12 10
如何根据第1列或按列分组显示第3列和第4列?
请看看[怎么问](http://stackoverflow.com/help/how-to-ask) – swe
@swe我改变了我的问题..如果仍然需要改进改变它或建议我。 –