如何根据值建立与列的关系

时间:2013-06-28 09:57:40

标签: sql ms-access

4我有这些表格:

day_shift                               
id_dshift   dshift_name on_duty off_duty    in_start    in_end  out_start   out_end     workday
1001        ds_normal   7:00    15:00       7:00        10:00   11:00       20:00       1
1002        ds_Saturday 7:00    14:00       7:00        10:00   11:00       14:00       1

week_shift
id_wshift   wshift_name   mon   tue     wed     thu     fri     sat     sun
2001        ws_normal     1001  1001    1001    1001    1001    1002    1001
2002        ws_2013_w1    0     1001    1001    1001    1001    1001    0
2003        ws_2013_w2    1003  1001    1001    1001    1001    1002    1001

daily_attendance                    
emp_id  checkdate   in      out     emp_shift_id
10      15/06/2013  7:10    15:05   2001        <-- saturday
10      16/06/2013  7:05    15:03   2001        <-- sunday

我想要的是得到这样的结果:

emp_id  checkdate   in       out    on_duty off_duty
10      15/06/2013  7:10    15:05   07:00   14:00
10      16/06/2013  7:30    14:30   07:00   15:00

在daily_attendance的第一行,因为工作日是星期六,所以我想得到week_shift.sat(1002)的值 如果工作日是星期日,我想获得week_shift.sun(1001)的价值

所以我从day_shift获得on_duty和off_duty值

如何在查询中执行此操作?

1 个答案:

答案 0 :(得分:2)

这里的技巧是在Access中创建一个名为[week_shift_transformed]的已保存查询,将[week_shift]表转换为一周中每一天的单独行:

SELECT id_wshift, wshift_name, 1 AS [weekday], [sun] as id_dshift FROM week_shift
UNION ALL
SELECT id_wshift, wshift_name, 2 AS [weekday], [mon] as id_dshift FROM week_shift
UNION ALL
SELECT id_wshift, wshift_name, 3 AS [weekday], [tue] as id_dshift FROM week_shift
UNION ALL
SELECT id_wshift, wshift_name, 4 AS [weekday], [wed] as id_dshift FROM week_shift
UNION ALL
SELECT id_wshift, wshift_name, 5 AS [weekday], [thu] as id_dshift FROM week_shift
UNION ALL
SELECT id_wshift, wshift_name, 6 AS [weekday], [fri] as id_dshift FROM week_shift
UNION ALL
SELECT id_wshift, wshift_name, 7 AS [weekday], [sat] as id_dshift FROM week_shift

那会给你

id_wshift  wshift_name  weekday  id_dshift
---------  -----------  -------  ---------
2001       ws_normal          1  1001     
2002       ws_2013_w1         1  0        
2003       ws_2013_w2         1  1001     
2001       ws_normal          2  1001     
2002       ws_2013_w1         2  0        
2003       ws_2013_w2         2  1003     
2001       ws_normal          3  1001     
2002       ws_2013_w1         3  1001     
2003       ws_2013_w2         3  1001     
2001       ws_normal          4  1001     
2002       ws_2013_w1         4  1001     
2003       ws_2013_w2         4  1001     
2001       ws_normal          5  1001     
2002       ws_2013_w1         5  1001     
2003       ws_2013_w2         5  1001     
2001       ws_normal          6  1001     
2002       ws_2013_w1         6  1001     
2003       ws_2013_w2         6  1001     
2001       ws_normal          7  1002     
2002       ws_2013_w1         7  1001     
2003       ws_2013_w2         7  1002  

然后你可以使用这样的查询:

SELECT da.emp_id, da.checkdate, da.in, da.out, ds.on_duty, ds.off_duty
FROM
    daily_attendance da
    INNER JOIN
    (
        week_shift_transformed wtt
        INNER JOIN
        day_shift ds
            ON ds.id_dshift = wtt.id_dshift
    )
        ON wtt.weekday = Weekday(da.checkdate)
            AND wtt.id_wshift = da.emp_shift_id