我应该使用哪个sql join查询?

时间:2013-05-14 05:08:42

标签: sql sql-server sql-server-2008

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

这是问题所在:

我有2张桌子,第一张是

员工:

empID name  branchID   etc
 1    ab      1        ...
 2    abc     4        ...
 3    ad      4        ...

,第二个表是

employeeAttendance:

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管理工作室2008,这很有趣,我觉得这很容易但我无法完成它,我试图使用LEFT OUTER JOIN,RIGHT OUTER JOIN,INNER JOIN,甚至CROSS加入,但他们都没有给我我想要的结果..

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

我在这里想念一些东西吗? 抱歉,如果这个问题很愚蠢,对不好的英语抱歉:)

4 个答案:

答案 0 :(得分: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 :(得分: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上的演示。

答案 2 :(得分: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

答案 3 :(得分: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

<强> Results

| 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) |