Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your tables appear small enough to do as I have done here. First, the inner-most query starts with all employees and did TWO left-joins immediately to the appointment table... By getting a MAX() of the appointments less than the date in question gets the "Previous Appointment", and getting the MIN() appointment AFTER the date in question gets the "Next Appointment". So now, for a single person, I have both their ID and possible previous and next appointments based on their specific times.</p> <p>Now, I take that result and re-join first to the appointment tables (left joined again) but this time based on same person (Terp_ID) AND their respective Previous and Next appointments date/time. This would only be a problem if you had multiple entries with the exact same date/time for a single person, and that would just result in multiple records.</p> <p>So now, I have each person with the specifics of previous and next appointments available.</p> <p>The rest is simple joining to the other tables to get only employee status of "Active", and the skill set of "Arabic" criteria (which I have at their respective JOIN criteria), otherwise you could just move these to a WHERE clause.</p> <p>As for the "Date/Time" basis of the query, I used @variable once so it could be used against both left-joins to appointments. Finally, I grabbed the respective fields you wanted. This SHOULD work, yet without your data, might need some tweaking.</p> <pre><code>SELECT EmpPrevNext.Employee_ID, EmpPrevNext.PrevApnt, EmpPrevNext.NextApnt, concat(Emp2.emp_firstname, ' ', Emp2.emp_lastname) as names, if ( Emp2.emp_gender = 1, 'Male', 'Female' ) as emp_gender, Emp2.emp_work_telephone, Emp2.emp_hm_telephone, Emp2.emp_work_email, Emp2.custom7, Emp2.city_code, Emp2.provin_code, cast( concat( DATE_FORMAT(PriorApp2.app_datetime, '%H:%i'), ' ', PriorApp2.app_facility) as char(100)) as PriorAppointment, cast( concat( DATE_FORMAT(NextApp2.app_datetime, '%H:%i'), ' ', NextApp2.app_facility) as char(100)) as NextAppointment, EStat.`name` as EmployeeStatus, Skill.`name` as SkillName FROM ( SELECT Emp.Employee_ID, MAX( PriorApp.app_DateTime ) as PrevApnt, MIN( NextApp.app_DateTime ) as NextApnt from ( select @DateBasis := '06/26/13 at 3:20 PM' ) sqlvars, hs_hr_employee as Emp LEFT JOIN li_appointments.li_appointments as PriorApp ON Emp.Employee_ID = NextApp.Terp_ID AND PriorApp.app_DateTime &lt;= @DateBasis LEFT JOIN li_appointments.li_appointments as NextApp ON Emp.Employee_ID = NextApp.Terp_ID AND NextApp.app_DateTime &gt; @DateBasis group by Emp.Employee_ID ) EmpPrevNext LEFT JOIN li_appointments.li_appointments as PriorApp2 ON EmpPrevNext.Employee_ID = PriorApp2.Terp_ID AND EmpPrevNext.PrevApnt = PriorApp2.app_DateTime LEFT JOIN li_appointments.li_appointments as NextApp2 ON EmpPrevNext.Employee_ID = NextApp2.Terp_ID AND EmpPrevNext.NextApnt = NextApp2.app_DateTime JOIN hs_hr_employee as Emp2 ON EmpPrevNext.Employee_ID = Emp2.Employee_ID JOIN orangehrm_li.ohrm_employment_status as EStat ON Emp2.Emp_Status = EStat.ID AND EStat.`name` = 'Active' JOIN hs_hr_emp_skill as EmpSkill ON Emp2.emp_number = EmpSkill.emp_number JOIN ohrm_skill as Skill on EmpSkill.skill_id = Skill.id AND Skill.`name` like '%Arabic%' order by rand(); </code></pre> <p>Make sure your appointment table has an index on (Terp_ID, app_datetime )</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload