我应该使用哪种 SQL 连接查询?

3
我是一名使用复杂查询方面的新手,所以我在这里有些困惑。
以下是问题:
我有两个表,第一个是员工表:
employee:
empID name  branchID   etc
 1    ab      1        ...
 2    abc     4        ...
 3    ad      4        ...

第二个表格是

员工考勤:

empID   attDate     hourIn  hourOut  etc
  1    05-06-2013    12.00   14.00   ...
  1    05-07-2013    10.00   14.00   ...
  1    05-10-2013    09.00   12.00   ...
  2    05-06-2013    08.00   14.00   ...
  2    05-10-2013    08.00   10.00   ...
  3    05-09-2013    11.00   15.00   ...

我的目标是创建以下视图:

empID name   attDate     hourIn  hourOut  etc
  1    ab  05-06-2013    12.00   14.00   ...
  2    abc 05-06-2013    08.00   14.00   ...
  3    ad  05-06-2013    null    null    ...
  1    ab  05-07-2013    10.00   14.00   ...
  2    abc 05-07-2013    null    null    ...
  3    ad  05-07-2013    null    null    ...
  1    ab  05-09-2013    null    null    ...
  2    abc 05-09-2013    null    null    ...
  3    ad  05-09-2013    11.00   15.00   ...
  1    ab  05-10-2013    09.00   12.00   ...
  2    abc 05-10-2013    08.00   10.00   ...
  3    ad  05-10-2013    null    null    ...

我使用的是SQL Server Management Studio 2008,感觉很简单,但最终我却做不到。我尝试了左外连接、右外连接、内连接,甚至是交叉连接,但没有一个能给我想要的结果。
几乎让我得到答案的是交叉连接,但ID不匹配,因为交叉连接没有使用ON子句。当我添加了WHERE子句后,它自动变成了内连接。
所以我在这里错过了什么吗?如果这个问题很愚蠢,请见谅,并对我的糟糕英语表示抱歉 :)

你必须同时使用CROSS JOINOUTER JOIN才能获得这个结果集。在我的答案中有工作演示。 - Aziz Shaikh
是的,我已经尝试过了,它可以正常工作。谢谢你的时间 :) - Alvin Setiawan
4个回答

4
 WITH DateList AS(
 SELECT DISTINCT E.EmpiD,E.Name,EA.AttDate FROM EmployeeAttendance EA
 CROSS JOIN Employee E )

 SELECT
    DL.empID,
    DL.name,
    DL.attDate,
    EA.hourIn,
    EA.hourOut,
    EA.etc
FROM DateList DL
LEFT OUTER JOIN EmployeeAttendance EA
ON DL.EmpID = EA.EmpID AND 
DL.AttDate = EA.AttDate
ORDER BY DL.AttDate,DL.EmpId

SQL Fiddle

拉杰


1

这里是您要的:

SELECT e.empID, name, attDay, hourIn, hourOut
FROM employee e
CROSS JOIN (SELECT distinct attDate AS attDay FROM employeeAttendance) AS allDates
LEFT OUTER JOIN employeeAttendance att
ON e.empID = att.empID and attDay = attDate

SQLFiddle上的演示。


0
使用 FULL OUTER JOIN。
SELECT employee.empID, employee.name,   employeeAttendance.attDate,employeeAttendance.hourIn,  employeeAttendance.hourOut,  employeeAttendance.etc 
FROM employee  
FULL OUTER JOIN employeeAttendance on employee.empID= employeeAttendance.empID

好的,我希望结果显示所有员工成员,在employeeAttendance.attDate中的每个日期。无论他们在employeeAttendance中是否有行。 - Alvin Setiawan

0
尝试这个:

SQL Fiddle

查询

select a.empID, a.name,   employeeAttendance.attDate,employeeAttendance.hourIn,
employeeAttendance.hourOut
from employeeAttendance full join 

(select empID, name,  branchID,attDate from emp
, (select distinct attDate from employeeAttendance)b)a 

on employeeAttendance.empID = a.empID  and employeeAttendance.attDate=a.attDate
order by empid,attDate desc

结果:

| EMPID | NAME |    ATTDATE | HOURIN | HOUROUT |
------------------------------------------------
|     1 |   ab | 05-10-2013 |  09.00 |   12.00 |
|     1 |   ab | 05-06-2013 |  12.00 |   14.00 |
|     1 |   ab |     (null) | (null) |  (null) |
|     2 |  abc | 05-10-2013 |  08.00 |   10.00 |
|     2 |  abc | 05-06-2013 |  08.00 |   14.00 |
|     2 |  abc |     (null) | (null) |  (null) |
|     3 |   ad | 05-09-2013 |  11.00 |   15.00 |
|     3 |   ad |     (null) | (null) |  (null) |
|     3 |   ad |     (null) | (null) |  (null) |

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接