我试图匹配员工日程表与报告日程表的结果。我需要输出一个报告,显示所有报告以及分配给他们的人。我遇到麻烦的地方是基于星期几。
我的ReportSchedule表看起来像这样:
╔══════════════╦══════════════╦══════╦══════╦══════╦══════╦══════╦══════╦════╗
║ 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表看起来像这样:
╔════════════╦══════════╦═════════════╦═══════════╦═══╦════╦═══╦════╦═══╦════╦════╗
║ 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 ║
╚════════════╩══════════╩══════════╩═══╩════╩═══╩════╩═══╩════╩════╝
我需要做些什么来获得我正在寻找的结果?
答案 0 :(得分:0)
添加where子句,其中report&lt;&gt; 0?此外,你在where语句中有报告id = report id - 我不确定这是必要的,因为那是你的join子句。还是sql的新手,所以我不确定。
答案 1 :(得分:0)
问题解决了。
我将原始查询插入到临时表中,然后运行以下内容并返回所需的数据。
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