I've been stuck on this for days.
I'm fairly new to PL/SQL (and SQL in general) and I'm trying to determine whether multiple conditions exist in a column, but not in a single record (which is what a WHERE statement checks).
For example, let's say I have a table of employees and their scheduled work shifts. And let's say I also have a table of their bids to work overtime, as follows (sorry about all the periods; I spent an hour trying to figure out how to format tables without them before giving up):
Table "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
Table "OvertimeBids"
EmployeeID ShiftID ShiftDate
1 4 3/21/16
2 4 3/21/16
What I want to do is populate a third table with the following results according to the rules listed below the table:
Table "Results"
EmployeeID ShiftID ShiftDate ApprovedYN
1 4 3/21/16 N
2 4 3/21/16 Y
RULES: If a particular employee is bidding on shift 4 on a particular date...
AND they are working shift 1 the day BEFORE that bid date
AND they are working shift 3 the day OF that bid date
AND they are working shift 1 the day AFTER that bid date
Then add an entry to the results table and populate "ApprovedYN" with an "N"
Otherwise, add an entry to the results table and populate "ApprovedYN" with a "Y"
AND and ORs don't seem to work because they apply all of their filters to each record, rather than applying each filter to the table individually.
Thanks!
Try:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.