3

我是使用复杂查询的菜鸟..所以我在这里有点困惑..

这是问题:

我有两张桌子,第一张是

员工 :

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,这很有趣,我觉得这很容易但我毕竟做不到,我尝试过使用左外连接,右外连接,内连接,甚至交叉连接,但是他们都没有给我我想要的结果..

几乎给我答案的一个是 CROSS JOIN 但 ID 不匹配,因为 CROSS JOIN 没有使用 ON 子句.. 当我添加 WHERE 时,它自动变为 INNER JOIN..

所以我在这里错过了什么吗?对不起,如果这个问题很愚蠢,对不起英语不好:)

4

4 回答 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小提琴

拉吉

于 2013-05-14T05:56:16.843 回答
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上的演示。

于 2013-05-14T06:18:11.517 回答
0

使用全外连接

SELECT employee.empID, employee.name,   employeeAttendance.attDate,employeeAttendance.hourIn,  employeeAttendance.hourOut,  employeeAttendance.etc 
FROM employee  
FULL OUTER JOIN employeeAttendance on employee.empID= employeeAttendance.empID
于 2013-05-14T05:19:21.580 回答
0

尝试这个 :

SQL小提琴

查询

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) |
于 2013-05-14T05:50:13.580 回答