ms-access合并具有类似Id的两个记录

时间:2016-09-04 07:08:11

标签: ms-access

我正在考虑在考勤应用中合并两个类似 ID 和类似日期的记录 我有这个表代码= 1 表示开始代码= 2 表示完成

<log in to server X>
echo <user>:<new password> | chpasswd
<adjust file permissions - see caveeats in link provided>
<log out>

我想以这种方式展示我的桌子

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

但实际上ms-access不支持 CASE WHEN 我也不能使用 IIF 而不是正确的结构

1 个答案:

答案 0 :(得分:0)

这可能是(采用逻辑方法忽略 Code ):

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]