2016-09-04 92 views
0

我想在出勤應用類似標識和類似日期合併兩個記錄 我有這個表,碼= 1種手段開始碼= 2意味着完成MS-訪問合併兩個記錄類似的標識

Id  Date   Time     Code 
---------------------------------------------------------------------------- 
1   2016.05.01  12:15     1 
1   2016.05.01  18:15     2 
2   2016.05.02  14:35     1 
2   2016.05.02  20:35     2 
x   xxxxxxxxxx  xxxxx     x 

,我想表明我的表在這樣

Id  Date   Time_Start   Time_Finish 
------------------------------------------------------------------------ 
1   2016.05.01  12:15    18:15 
2   2016.05.02  14:35    20:35 
x   xxxxxxxxxx  xxxxx    xxxxx 

,我發現這個查詢

SELECT Id,Date, 
MAX(CASE WHEN code =1 THEN Time END) AS Time_Start, 
MAX(CASE WHEN code =2 THEN Time END) AS Time_Finish 
FROM tbl_attendance 
GROUP BY Id, Date 

但實際上MS-訪問不支持CASE WHEN 還我不能在正確的結構

回答

0

,可以使用IIF的而不是(採取邏輯方法忽略代碼):

SELECT 
    Id, 
    [Date], 
    Min(CDate([Time])) AS Time_Start, 
    Max(CDate([Time])) AS Time_Finish 
FROM 
    tbl_attendance 
GROUP BY 
    Id, 
    [Date] 

爲了保證輸出的文字:

SELECT 
    Id, 
    [Date], 
    Format(Min(CDate([Time])), "hh:nn") AS Time_Start, 
    Format(Max(CDate([Time])), "hh:nn") AS Time_Finish 
FROM 
    tbl_attendance 
GROUP BY 
    Id, 
    [Date] 

或者:

SELECT 
    Id, 
    [Date], 
    Sum(CDate(IIf([Code] = 1, [Time], "0"))) AS Time_Start, 
    Sum(CDate(IIf([Code] = 2, [Time], "0"))) AS Time_Finish 
FROM 
    tbl_attendance 
GROUP BY 
    Id, 
    [Date] 
+0

這是不可能的上班時間,因爲該類型字符 –

+0

然後轉換爲日期值。請參閱編輯。 – Gustav

+0

坦克,但我仍然需要與許多其他reasons.is代碼工作有什麼辦法嗎? –