SQL查询在给定日期列表的员工时间表中显示状态为“未输入”

时间:2015-03-09 16:54:57

标签: sql ms-access

我一直在网上寻找答案,但没有运气。我刚创建了一个帐户&我是新来的,所以请原谅我,如果帖子搞砸了。

我将提到查询所需的重要表格,

我在MS Access中有3个表(暂时迁移到SQL Server):

  • T_Employee - > Emp_ID,Emp_name,...
  • T_Custom_Calendar_Dates - > YearNo,MonthNo,WeekNo,Week_Start_Date,Week_End_Date
  • T_Timesheet_Header - > Emp_ID,Week_End_Date,...

  1. T_Employee表将包含员工列表,Emp_ID为主键

  2. T_Custom_calendar_Dates表包含以下示例数据:

    Year_No   Month_No      WeekNo     Week_Start_Date    Week_End_Date
    ---------------------------------------------------------------------
    2015           1           1        02/01/2015         08/01/2015
    2015           1           2        09/01/2015         15/01/2015
    2015           1           3        16/01/2015         22/01/2015
    2015           1           4        23/01/2015         29/01/2015
    2015           1           5        30/01/2015         05/02/2015
    2015           2           1        06/02/2015         12/02/2015
    2015           2           2        13/02/2015         19/02/2015
    2015           2           3        20/02/2015         26/02/2015
    2015           2           4        27/02/2015         05/03/2015
    and so on till Month_No reaches 12
    
  3. T_Timesheet_Header表应该包含EMP_IDWeek_End_Date,如下所示:

    Emp_ID    Week_End_Date     ,other columns
    ----------------------------------
      1       08/01/2015
      1       15/01/2015
      1       29/01/2015
    
  4. 注意我已跳过输入2015年1月22日的记录

    所以我要查找的查询是显示如下内容:

    Week_End_Date           Emp_ID         Status
    08/01/2015                1            Entered
    15/01/2015                1            Entered
    22/01/2015                1            Not Entered
    29/01/2015                1            Entered
    08/01/2015                2            Not Entered
    15/01/2015                2            Not Entered
    22/01/2015                2            Not Entered
    29/01/2015                2            Not Entered
    
    {p>等来自Week_end_date表的T_Custom_Calendar_Dates列表的其余部分。

    我不确定是否可以不创建临时表

    我试着摆弄SQL连接但没有运气,希望有一些SQL大师来帮我吧

2 个答案:

答案 0 :(得分:1)

首先,您需要获得不同的emp_idWeek_End_dates的组合,以便考虑所有组合。您可以使用CROSS JOIN

执行此操作
SELECT
    t1.Week_End_Date,
    t2.Emp_ID
FROM
    T_Custom_Calendar_Dates As T1,
        (
            SELECT Emp_ID FROM T_Timesheet_Header GROUP BY Emp_ID
        ) as T2

您可以将其用作子查询,以加入T_Timesheet_Header表,以确定Week_End_Date EmpID / T_Timesheet_Header的唯一组合>

SELECT
    T3.Week_End_Date,
    T3.Emp_ID,
    IIF(ISNULL(T4.Emp_ID), "Not Entered", "Entered") AS [Status]
FROM
    (
            SELECT
                t1.Week_End_Date,
                t2.Emp_ID
            FROM
                T_Custom_Calendar_Dates As T1,                  
                (
                    SELECT Emp_ID FROM T_Timesheet_Header GROUP BY Emp_ID
                ) as T2
    ) as T3
    LEFT JOIN T_Timesheet_Header as T4 ON
        T3.Week_End_DAte = T4.Week_End_Date AND
        T3.Emp_ID = T4.Emp_ID

答案 1 :(得分:0)

我不确定但是试一试

select tccd.Week_End_Date, 
  tth.Emp_ID, 
  SWITCH(IsNull(tth.Emp_ID), 'Not Entered',
              TRUE, 'Entered') AS Status
from T_Custom_calendar_Dates tccd left join T_Timesheet_Header tth 
on tccd.Emp_ID = th.Emp_ID