【问题标题】:How to combine results of 3 Joins?如何组合 3 个 Join 的结果?
【发布时间】:2017-12-03 09:23:17
【问题描述】:

我正在尝试为每位员工创建一个表,该表将为每位员工提供在家/不在/在家工作的信息。为此,我正在编写一个使用 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

我得到的当前输出是: 姓名|现在,姓名|缺席,姓名|在家工作 但我希望它为: 姓名|现在|缺席|在家工作

【问题讨论】:

  • 你可以使用UNION ALL
  • 试过联合,但结果是成行的..我不想要那个..
  • 啊,我想你的意思是把它们组合成列Name, Absent, Present, WorkFromHome
  • @Sami 是的:)
  • 使用Subqueries 而不是UNION ALL

标签: sql sql-server join


【解决方案1】:

使用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;

【讨论】:

  • 您的查询运行良好。虽然我面临一个问题..有些员工甚至没有缺勤一次,或者有些员工没有在家工作一次,所以连接中没有显示记录..有没有办法让我也可以通过显示值 0 或类似的值来显示他们的记录。我没有在 Where 子句中使用 Employee.Name,而是检查所有员工。
  • @DelMonte 将他们加入您的员工表并使用LEFT JOIN
  • 我试过了,但问题是它返回的员工 ID 也为 NULL。有什么办法可以解决这个问题?
【解决方案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 将返回所有记录。

【讨论】:

    【解决方案3】:

    使用 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
    

    【讨论】:

    • 您确定结果的格式为:Name|Present|Absent|WorkFromHome 而不是“行”格式
    猜你喜欢
    • 2020-10-12
    • 2016-10-07
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2019-02-13
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多