如何结合3个连接的结果?

时间:2017-12-03 09:23:17

标签: sql sql-server join

我正在尝试为每个员工创建一个表,该表将提供每个员工在家中出现/缺席/工作的信息。为此我正在编写一个使用JOINS获取记录的SQL查询。但是,我正在写3个Join语句,它们分别返回数据。如何编写一个查询,该查询将在一个(SELECT)查询中返回3个后续连接的结果?以下是Join语句:

Select EmployeeId, Employees.Name, COUNT(*) [Absent]
From Attendances
Left Join Employees
On Employees.EmployeeId = Attendances.EmpID And Attendances.IsAbsent = 1 and Employees.Name = 'John'
Where CAST(Attendances.InDateTime as Date) >= '2016-06-19 10:00:00.000' 
Group By EmployeeId, Employees.Name

select Employees.Name, COUNT(*) [Present]
from Employees
Inner Join Attendances
On Employees.UserId = Attendances.EmpID and Attendances.IsAbsent = 0
Where CAST(Attendances.InDateTime as Date) >= '2017-06-19 10:00:00.000' and Employees.Name='John'
Group By Employees.Name

select Employees.Name, COUNT(*) [WorkFromHome]
from Employees
Inner Join Attendances
On Employees.UserId = Attendances.EmpID and Attendances.IsWorkFromHome = 1
Where CAST(Attendances.InDateTime as Date) >= '2017-06-19 10:00:00.000' and Employees.Name='John'
Group By Employees.Name

我得到的当前输出是: 姓名|现在,姓名|缺席,姓名| WorkFromHome 但我想要它: 名称|当前|缺席| WorkFromHome

3 个答案:

答案 0 :(得分:2)

您可以使用UNION或UNION ALL。例如

SELECT * FROM TABLE WHERE NAME = 'Mohit'
UNION ALL
SELECT * FROM TABLE WHERE NAME = 'Jhon'
UNION ALL
SELECT * FROM TABLE WHERE NAME = 'Jeni'

UNION ALL将返回唯一记录。但UNION将返回所有记录。

答案 1 :(得分:2)

使用Subqueries获得所需的输出:

SELECT T1.ID,
       T1.Name,
       T1.[Absent],
       T2.[Present],
       T3.[WorkFromHome]
FROM
(
select ID, Employees.Name, COUNT(*) [Absent]
from Employees
Inner Join Attendances
On Employees.UserId = Attendances.EmpID and Attendances.IsAbsent = 1
Where CAST(Attendances.InDateTime as Date) >= '2017-06-19 10:00:00.000' and Employees.Name='John'
Group By Employees.Name, ID
) T1 JOIN
(
select ID, Employees.Name, COUNT(*) [Present]
from Employees
Inner Join Attendances
On Employees.UserId = Attendances.EmpID and Attendances.IsAbsent = 0
Where CAST(Attendances.InDateTime as Date) >= '2017-06-19 10:00:00.000' and Employees.Name='John'
Group By Employees.Name, ID
) T2 ON T1.ID = T2.ID JOIN
(
select ID, Employees.Name, COUNT(*) [WorkFromHome]
from Employees
Inner Join Attendances
On Employees.UserId = Attendances.EmpID and Attendances.IsWorkFromHome = 1
Where CAST(Attendances.InDateTime as Date) >= '2017-06-19 10:00:00.000' and Employees.Name='John'
Group By Employees.Name, ID
) T3 ON T2.ID = T3.ID;

答案 2 :(得分:1)

使用UNION ALL获取此类组合数据

select Employees.Name, 'A' Attendance, COUNT(*) [Count]
from Employees
Inner Join Attendances
On Employees.UserId = Attendances.EmpID and Attendances.IsAbsent = 1
Where CAST(Attendances.InDateTime as Date) >= '2017-06-19 10:00:00.000' and 
Employees.Name='John'
Group By Employees.Name
UNION ALL
select Employees.Name, 'P' Attendance, COUNT(*) [Count]
from Employees
Inner Join Attendances
On Employees.UserId = Attendances.EmpID and Attendances.IsAbsent = 0
Where CAST(Attendances.InDateTime as Date) >= '2017-06-19 10:00:00.000' and 
Employees.Name='John'
Group By Employees.Name
UNION ALL
select Employees.Name, 'WFH' Attendance, COUNT(*) [Count]
from Employees
Inner Join Attendances
On Employees.UserId = Attendances.EmpID and Attendances.IsWorkFromHome = 1
Where CAST(Attendances.InDateTime as Date) >= '2017-06-19 10:00:00.000' and 
Employees.Name='John'
Group By Employees.Name