I am trying to automate production of a roster based on leave dates and working preferences. I have generated some data to work with, and I now have two tables, one with a list of individuals and their preferences for working on particular days of the week(e.g. some prefer to work on a Tuesday, others only every other Wednesday etc and another with leave dates for individuals. That looks like this, where firstpref
and secondpref
represent weekdays with Mon = 1, Sun = 7 and firstprefclw
represents a marker for which week of a 2 week pattern someone prefers (0 = no pref, 1 = wk 1 preferred, 2 = wk2 preferred)
initials | firstpref | firstprefclw | secondpref | secondprefclw KP | 3 | 0 | 1 | 0 BD | 2 | 1 | 1 | 0 LW | 3 | 0 | 4 | 1
Then there is atable leave_entries
, which basically has the intials, a start date and an end date for each leave request.
Finally there is a pre-calculated clwdates
table, which contains a marker (a 1
or 2
) for each day in one of its columns as to what week of the roster pattern it is.
I have run this query:
SELECT @tdate, DATE_FORMAT(@tdate,'%W') AS whatDay, GROUP_CONCAT(t1.initials separator ',') AS available
FROM people AS t1
WHERE ((t1.firstpref = (DAYOFWEEK(@tdate))-1
AND (t1.firstprefclw = 0 OR (t1.firstprefclw = (SELECT c_dates.clw from clwdates AS c_dates LIMIT i,1))))
OR (t1.secondpref = (DAYOFWEEK(@tdate))-1
AND (t1.secondprefclw = 0 OR (t1.secondprefclw = (SELECT c_dates.clw from clwdates AS c_dates LIMIT i,1)))
OR ((DAYOFWEEK(@tdate))-1 IN (0,5,6))
AND t1.initials NOT IN (SELECT initials FROM leave_entries WHERE @tdate BETWEEN leave_entries.start_date and leave_entries.end_date)
);
My output from that is a list of dates with initials of the pattern:
2018-01-03;Wednesday;KP,LW,TH
My desired output is
2018-01-03;Wednesday;KP
where the initials of the person have been randomly selected from the list of available people generated by the first set of SELECT
s.
My question: how can I randomly select an individual's initials from the available ones and create a table which is basically date ; random_person
?
Aucun commentaire:
Enregistrer un commentaire