Guest User

Untitled

a guest
Oct 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. -- Behavior of Adopted Customers
  2.  
  3. SELECT
  4. --count(distinct location_id)
  5. *
  6. FROM
  7. (SELECT
  8. employees.email as office_manager_email,
  9. customers.name as customer_name,
  10. locations.address as location_address,
  11. employees.created_at as first_invitation_sent_date,
  12. employees.invitation_accepted_at as first_login_date,
  13. locations.id as location_id,
  14. shift_notes_count,
  15. review_count,
  16. tasks_created
  17. FROM
  18. employees
  19. INNER JOIN
  20. (SELECT
  21. employees.id as employee_id,
  22. count(comments.id) as shift_notes_count
  23. from
  24. employees
  25. left join
  26. comments on commenter_id = employees.id
  27. and
  28. commenter_type = 'Employee'
  29. and
  30. commentable_type = 'Cleaning'
  31. where
  32. employees.role = 'office_manager'
  33. group by employees.id)
  34. as shift_notes ON shift_notes.employee_id = employees.id
  35. INNER JOIN
  36. (SELECT
  37. employees.id as employee_id,
  38. count(reviews.id) as review_count
  39. FROM
  40. employees
  41. LEFT JOIN
  42. reviews on reviews.employee_id = employees.id
  43. GROUP BY
  44. employees.id)
  45. AS reviews_left ON reviews_left.employee_id = employees.id
  46. INNER JOIN
  47. (SELECT
  48. employees.id as employee_id,
  49. count(task_events.id) as tasks_created
  50. FROM
  51. employees
  52. LEFT JOIN
  53. task_events on task_events.actor_id = employees.id
  54. AND
  55. actor_type = 'employee'
  56. AND
  57. event_action = 'create'
  58. GROUP BY
  59. employees.id)
  60. AS tasks_created ON tasks_created.employee_id = employees.id
  61. INNER JOIN
  62. employees_locations ON employees.id = employees_locations.employee_id
  63. INNER JOIN
  64. locations ON employees_locations.location_id = locations.id
  65. INNER JOIN
  66. customers ON locations.customer_id = customers.id
  67. WHERE
  68. employees.role = 'office_manager'
  69. AND
  70. employees.created_at > '2017.09.13') as foo;
Add Comment
Please, Sign In to add comment