我已经坚持了好几天。
我是PL / SQL(以及一般的SQL)的新手,我正在尝试确定列中是否存在多个条件,而不是单个记录(这是WHERE语句检查的内容)。< / p>
例如,假设我有一张员工表及其预定的工作班次。让我们说我还有一张他们加班加点的表格,如下所示(对所有时期都很抱歉;我花了一个小时试图弄清楚如何在没有它们之前格式化表格):
表“ScheduledShifts”
EmployeeID ShiftID ShiftDate
1 1 3/20/16
1 3 3/21/16
1 1 3/22/16
2 1 3/20/16
2 1 3/21/16
2 2 3/22/16
表“OvertimeBids”
EmployeeID ShiftID ShiftDate
1 4 3/21/16
2 4 3/21/16
我想要做的是根据表格下面列出的规则填充第三张表格,其中包含以下结果:
表“结果”
EmployeeID ShiftID ShiftDate ApprovedYN
1 4 3/21/16 N
2 4 3/21/16 Y
规则: 如果特定员工在特定日期的第4班出价...... 他们在投标日期之前的那一天工作1班 并且他们在该出价日期的第3天工作 并且他们在该出价日期之后的第1天工作
然后在结果表中添加一个条目,并用“N”填充“ApprovedYN”
否则,在结果表中添加一个条目,并用“Y”填充“ApprovedYN”
AND和OR似乎不起作用,因为它们将所有过滤器应用于每个记录,而不是单独将每个过滤器应用于表。
谢谢!
答案 0 :(得分:0)
尝试:
select employeeID,ShiftID,ShiftDate,
CASE WHEN EXISTS(
select *
from ScheduledShifts ss1
join ScheduledShifts ss2 on ss1.EmployeeID = ss2.EmployeeID
join ScheduledShifts ss3 on ss2.EmployeeID = ss3.EmployeeID
where
ss1.EmployeeID = ob.EmployeeID
/* AND they are working shift 1 the day BEFORE that bid date */
AND ss1.ShiftID = 1 AND ss1.shiftdate = ob.shiftdate - 1
/* AND they are working shift 3 the day OF that bid date */
AND ss2.ShiftID = 3 AND ss2.shiftdate = ob.shiftdate
/* AND they are working shift 1 the day AFTER that bid date */
AND ss3.ShiftID = 1 AND ss3.shiftdate = ob.shiftdate + 1
)
/* Then add an entry to the results table
and populate "ApprovedYN" with an "N" */
THEN 'N'
/* Otherwise, add an entry to the results table
and populate "ApprovedYN" with a "Y" */
ELSE 'Y'
END As ApprovedYN
from OvertimeBids ob;