1
新到SQL,使用MS SQL Sever的管理工作室AdventureWorks示例数据库:如何将两个select语句组合成一个表,并使用两个独立的列?
试图在两个SELECT
报表每个包含男女员工的COUNT
结合起来。 我可以使用UNION ALL将两个计数返回到同一个表上的两行。
SELECT COUNT(HumanResources.Employee.Gender) AS 'Male Employees'
FROM HumanResources.Employee
WHERE Employee.Gender = 'M'
UNION ALL
SELECT COUNT(HumanResources.Employee.Gender) AS 'Female Employees'
FROM HumanResources.Employee
WHERE Employee.Gender = 'F';
但是我试图让每个M/F的COUNT
在两个单独的列。管理得到两个单独的列出现,但计数不存在。
SELECT Set1.[Male Employees], Set2.[Female Employees]
FROM
(
SELECT COUNT(Employee.Gender) AS 'Male Employees'
FROM HumanResources.Employee
WHERE Employee.Gender = 'M'
) as Set1
INNER JOIN
(
SELECT COUNT(Employee.Gender) AS 'Female Employees'
FROM HumanResources.Employee
WHERE Employee.Gender = 'F'
) as Set2
on Set1.[Male Employees] = Set2.[Female Employees]
我觉得我失去了一些东西明显..
我看,看起来比使用'INNER JOIN'更有效率 – girthquake