2013-05-14 65 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