Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Person_id | employee_id | appointment_time
- ----------+-------------+-----------------
- int | int | date
- SELECT
- qLast.person_id,
- qLast.employee_id,
- qLast.LastDate,
- qPrevious.employee_id,
- qPrevious.PreviousDate
- FROM
- (
- SELECT
- app.person_id,
- app.employee_id,
- Max(app.appointment_time) AS LastDate
- FROM
- app
- GROUP BY
- app.person_id,
- app.employee_id
- HAVING
- app.person_id <> 0
- AND app.employee_id = 235
- ) qLast
- LEFT JOIN (
- SELECT
- qSub.person_id,
- app.employee_id,
- qSub.MaxOfappointment_time AS PreviousDate
- FROM
- (
- SELECT
- app.person_id,
- Max(app.appointment_time) AS MaxOfappointment_time
- FROM
- app
- GROUP BY
- app.person_id,
- app.employee_id
- HAVING
- app.person_id <> 0
- AND app.employee_id <> 235
- ) qSub
- INNER JOIN app ON (
- qSub.MaxOfappointment_time = app.appointment_time
- )
- AND (qSub.person_id = app.person_id)
- ) qPrevious ON qLast.person_id = qPrevious.person_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement