2013-05-14 66 views
3

我在使用复杂的查询小白..所以我是一个有点困惑在这里..我应该使用哪个sql连接查询?

这里的问题:

我有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 Management Studio中2008年,这很有趣,我觉得这是很容易的,但我不能让它毕竟,我试图使用左外部连接,右外部连接,内部连接,甚至交叉连接,但他们都没有给我我想要的结果..

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

我也错过了这里的东西吗? 对不起,如果这个问题很可笑,遗憾的英语不好:)

+0

您必须同时使用'CROSS JOIN'和'OUTER JOIN'来实现这个结果集。在[我的答案]中的工作演示(http://stackoverflow.com/a/16536344/309086)。 – 2013-05-14 06:19:45

+0

是的,我已经尝试了,它的工作..谢谢你的时间:) – 2013-05-14 06:46:23

回答

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

拉吉

+0

thx,它工作! 非常感谢你拉吉:) – 2013-05-14 06:33:13

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 
+0

将不会工作兄弟... 给定的结果是完全一样的右外连接.. – 2013-05-14 05:24:18

+0

你的结果有任何逻辑? – AnandPhadke 2013-05-14 05:26:37

+0

好的,我希望结果显示employeeAttendance.attDate中的每个日期中的所有员工成员。无论他们是否在职员出席排队。 – 2013-05-14 05:32:33

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

yeahh ..这个作品太.. :) 再次感谢大家 – 2013-05-14 06:44:57