Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. Person_id | employee_id | appointment_time
  2. ----------+-------------+-----------------
  3. int | int | date
  4.  
  5. SELECT
  6. qLast.person_id,
  7. qLast.employee_id,
  8. qLast.LastDate,
  9. qPrevious.employee_id,
  10. qPrevious.PreviousDate
  11. FROM
  12. (
  13. SELECT
  14. app.person_id,
  15. app.employee_id,
  16. Max(app.appointment_time) AS LastDate
  17. FROM
  18. app
  19. GROUP BY
  20. app.person_id,
  21. app.employee_id
  22. HAVING
  23. app.person_id <> 0
  24. AND app.employee_id = 235
  25. ) qLast
  26. LEFT JOIN (
  27. SELECT
  28. qSub.person_id,
  29. app.employee_id,
  30. qSub.MaxOfappointment_time AS PreviousDate
  31. FROM
  32. (
  33. SELECT
  34. app.person_id,
  35. Max(app.appointment_time) AS MaxOfappointment_time
  36. FROM
  37. app
  38. GROUP BY
  39. app.person_id,
  40. app.employee_id
  41. HAVING
  42. app.person_id <> 0
  43. AND app.employee_id <> 235
  44. ) qSub
  45. INNER JOIN app ON (
  46. qSub.MaxOfappointment_time = app.appointment_time
  47. )
  48. AND (qSub.person_id = app.person_id)
  49. ) qPrevious ON qLast.person_id = qPrevious.person_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement