Return 基于匹配时间和天数的值
Return values based matching times and days
我正在尝试匹配员工计划与报告计划的结果。我需要输出一份报告,显示所有报告以及分配给他们的人员。我遇到麻烦的地方是基于星期几。
我的 ReportSchedule table 看起来像这样:
╔══════════════╦══════════════╦══════╦══════╦══════╦══════╦══════╦══════╦════╗
║ ReportID ║ Time ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠══════════════╬══════════════╬══════╬══════╬══════╬══════╬══════╬══════╬════╣
║ 1001 ║ 06:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 1002 ║ 06:48:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║
╚══════════════╩══════════════╩══════╩══════╩══════╩══════╩══════╩══════╩════╝
我的 EmployeesSchedule table 看起来像这样:
╔════════════╦══════════╦═════════════╦═══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ EmployeeID ║ ReportID ║ ReportStart ║ ReportEnd ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠════════════╬══════════╬═════════════╬═══════════╬═══╬════╬═══╬════╬═══╬════╬════╣
║ 22001 ║ 1001 ║ 05:00:00 ║ 12:00:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22001 ║ 1002 ║ 05:00:00 ║ 12:00:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22001 ║ 1003 ║ 05:00:00 ║ 12:00:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1001 ║ 06:00:00 ║ 14:00:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22002 ║ 1002 ║ 06:00:00 ║ 14:00:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22002 ║ 1003 ║ 06:00:00 ║ 14:00:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
╚════════════╩══════════╩═════════════╩═══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
根据以上我需要的是这样的:
╔════════════╦══════════╦══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ EmployeeID ║ ReportID ║ Time ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠════════════╬══════════╬══════════╬═══╬════╬═══╬════╬═══╬════╬════╣
║ 22001 ║ 1001 ║ 06:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1002 ║ 06:48:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22001 ║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1003 ║ 07:18:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
╚════════════╩══════════╩══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
我运行的查询如下:
SELECT EmployeeSchedule.EmployeeID, ReportSchedule.ReportID, ReportSchedule.Time,
ReportSchedule.M, ReportSchedule.Tu, ReportSchedule.W, ReportSchedule.Th, ReportSchedule.F, ReportSchedule.Sa, ReportSchedule.Su
FROM ReportSchedule
INNER JOIN EmployeeSchedule on ReportSchedule.ReportID = EmployeeSchedule.ReportID
WHERE (
ReportSchedule.Time > EmployeeSchedule.ReportStart AND
ReportSchedule.Time < EmployeeSchedule.ReportEnd AND
(
(ReportSchedule.M=1) AND (ReportSchedule.M = EmployeeSchedule.M) OR
(ReportSchedule.Tu=1) AND (ReportSchedule.Tu = EmployeeSchedule.Tu) OR
(ReportSchedule.W=1) AND (ReportSchedule.W = EmployeeSchedule.W) OR
(ReportSchedule.Th=1) AND (ReportSchedule.Th = EmployeeSchedule.Th) OR
(ReportSchedule.F=1) AND (ReportSchedule.F = EmployeeSchedule.F) OR
(ReportSchedule.Sa=1) AND (ReportSchedule.Sa = EmployeeSchedule.Sa) OR
(ReportSchedule.Su=1) AND (ReportSchedule.Su = EmployeeSchedule.Su)
)
)
这返回的结果不是我要找的结果,因为它没有过滤掉一周中员工不做报告的日子。这是返回的内容:
╔════════════╦══════════╦══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ EmployeeID ║ ReportID ║ Time ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠════════════╬══════════╬══════════╬═══╬════╬═══╬════╬═══╬════╬════╣
║ 22001 ║ 1001 ║ 06:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1002 ║ 06:48:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22001 ║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║
║ 22002 ║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║
╚════════════╩══════════╩══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
我需要做什么才能得到我想要的结果?
添加 where 子句 where report <> 0?此外,您在 where 语句中有 report id = report id - 我不确定这是必要的,因为那是您的连接子句。 sql 还是新手,所以我不确定。
问题已解决。
我将原始查询插入临时 table,然后 运行 以下内容并返回所需的数据。
SELECT #Temp.EmployeeID, #Temp.ReportID, #Temp.Time,
CASE WHEN EmployeeSchedule.M = 0 THEN 0 ELSE 1 END AS M,
CASE WHEN EmployeeSchedule.Tu = 0 THEN 0 ELSE 1 END AS Tu,
CASE WHEN EmployeeSchedule.W = 0 THEN 0 ELSE 1 END AS W,
CASE WHEN EmployeeSchedule.Th = 0 THEN 0 ELSE 1 END AS Th,
CASE WHEN EmployeeSchedule.F = 0 THEN 0 ELSE 1 END AS F,
CASE WHEN EmployeeSchedule.Sa = 0 THEN 0 ELSE 1 END AS Sa,
CASE WHEN EmployeeSchedule.Su = 0 THEN 0 ELSE 1 END AS Su
FROM #Temp
INNER JOIN EmployeeSchedule on #Temp.ReportID = EmployeeSchedule.ReportID
我正在尝试匹配员工计划与报告计划的结果。我需要输出一份报告,显示所有报告以及分配给他们的人员。我遇到麻烦的地方是基于星期几。
我的 ReportSchedule table 看起来像这样:
╔══════════════╦══════════════╦══════╦══════╦══════╦══════╦══════╦══════╦════╗
║ ReportID ║ Time ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠══════════════╬══════════════╬══════╬══════╬══════╬══════╬══════╬══════╬════╣
║ 1001 ║ 06:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 1002 ║ 06:48:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║
╚══════════════╩══════════════╩══════╩══════╩══════╩══════╩══════╩══════╩════╝
我的 EmployeesSchedule table 看起来像这样:
╔════════════╦══════════╦═════════════╦═══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ EmployeeID ║ ReportID ║ ReportStart ║ ReportEnd ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠════════════╬══════════╬═════════════╬═══════════╬═══╬════╬═══╬════╬═══╬════╬════╣
║ 22001 ║ 1001 ║ 05:00:00 ║ 12:00:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22001 ║ 1002 ║ 05:00:00 ║ 12:00:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22001 ║ 1003 ║ 05:00:00 ║ 12:00:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1001 ║ 06:00:00 ║ 14:00:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22002 ║ 1002 ║ 06:00:00 ║ 14:00:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22002 ║ 1003 ║ 06:00:00 ║ 14:00:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
╚════════════╩══════════╩═════════════╩═══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
根据以上我需要的是这样的:
╔════════════╦══════════╦══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ EmployeeID ║ ReportID ║ Time ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠════════════╬══════════╬══════════╬═══╬════╬═══╬════╬═══╬════╬════╣
║ 22001 ║ 1001 ║ 06:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1002 ║ 06:48:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22001 ║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1003 ║ 07:18:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
╚════════════╩══════════╩══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
我运行的查询如下:
SELECT EmployeeSchedule.EmployeeID, ReportSchedule.ReportID, ReportSchedule.Time,
ReportSchedule.M, ReportSchedule.Tu, ReportSchedule.W, ReportSchedule.Th, ReportSchedule.F, ReportSchedule.Sa, ReportSchedule.Su
FROM ReportSchedule
INNER JOIN EmployeeSchedule on ReportSchedule.ReportID = EmployeeSchedule.ReportID
WHERE (
ReportSchedule.Time > EmployeeSchedule.ReportStart AND
ReportSchedule.Time < EmployeeSchedule.ReportEnd AND
(
(ReportSchedule.M=1) AND (ReportSchedule.M = EmployeeSchedule.M) OR
(ReportSchedule.Tu=1) AND (ReportSchedule.Tu = EmployeeSchedule.Tu) OR
(ReportSchedule.W=1) AND (ReportSchedule.W = EmployeeSchedule.W) OR
(ReportSchedule.Th=1) AND (ReportSchedule.Th = EmployeeSchedule.Th) OR
(ReportSchedule.F=1) AND (ReportSchedule.F = EmployeeSchedule.F) OR
(ReportSchedule.Sa=1) AND (ReportSchedule.Sa = EmployeeSchedule.Sa) OR
(ReportSchedule.Su=1) AND (ReportSchedule.Su = EmployeeSchedule.Su)
)
)
这返回的结果不是我要找的结果,因为它没有过滤掉一周中员工不做报告的日子。这是返回的内容:
╔════════════╦══════════╦══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ EmployeeID ║ ReportID ║ Time ║ M ║ Tu ║ W ║ Th ║ F ║ Sa ║ Su ║
╠════════════╬══════════╬══════════╬═══╬════╬═══╬════╬═══╬════╬════╣
║ 22001 ║ 1001 ║ 06:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║
║ 22002 ║ 1002 ║ 06:48:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║
║ 22001 ║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║
║ 22002 ║ 1003 ║ 07:18:00 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║
╚════════════╩══════════╩══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
我需要做什么才能得到我想要的结果?
添加 where 子句 where report <> 0?此外,您在 where 语句中有 report id = report id - 我不确定这是必要的,因为那是您的连接子句。 sql 还是新手,所以我不确定。
问题已解决。
我将原始查询插入临时 table,然后 运行 以下内容并返回所需的数据。
SELECT #Temp.EmployeeID, #Temp.ReportID, #Temp.Time,
CASE WHEN EmployeeSchedule.M = 0 THEN 0 ELSE 1 END AS M,
CASE WHEN EmployeeSchedule.Tu = 0 THEN 0 ELSE 1 END AS Tu,
CASE WHEN EmployeeSchedule.W = 0 THEN 0 ELSE 1 END AS W,
CASE WHEN EmployeeSchedule.Th = 0 THEN 0 ELSE 1 END AS Th,
CASE WHEN EmployeeSchedule.F = 0 THEN 0 ELSE 1 END AS F,
CASE WHEN EmployeeSchedule.Sa = 0 THEN 0 ELSE 1 END AS Sa,
CASE WHEN EmployeeSchedule.Su = 0 THEN 0 ELSE 1 END AS Su
FROM #Temp
INNER JOIN EmployeeSchedule on #Temp.ReportID = EmployeeSchedule.ReportID