Skip to content

Instantly share code, notes, and snippets.

@tance77
Created February 28, 2018 18:37
Show Gist options
  • Save tance77/3cdaf71e5cfbb86f275aea795dcb531b to your computer and use it in GitHub Desktop.
Save tance77/3cdaf71e5cfbb86f275aea795dcb531b to your computer and use it in GitHub Desktop.
Horrible SQL needs optimization
SELECT
innerData.*,
vp.salesforce_number AS vp_sales_force_number,
vp.first_name AS vp_first_name,
vp.last_name AS vp_last_name,
client_user.salesforce_number AS client_sales_force_number,
client_user.first_name AS client_first_name,
client_user.last_name AS client_last_name,
team.name AS team_name,
departments.title AS department_name
FROM (SELECT
boundedTasks.id AS task_id,
mv.va_id,
mv.client_id,
mv.department_id,
mv.team_id,
boundedTasks.today_date,
boundedTasks.time_from,
boundedTasks.time_to,
boundedTasks.time_started,
boundedTasks.time_completed,
mv.duration,
boundedTasks.status
FROM
(SELECT *
FROM
tasks AS t
WHERE
t.today_date >= '2016-02-01') AS boundedTasks
INNER JOIN
mod_va_client AS mv ON mv.va_id = boundedTasks.client_va_id
WHERE
mv.va_id IN (SELECT u.id
FROM
users AS u
INNER JOIN
mod_va_client AS mvc ON u.id = mvc.va_id
WHERE
mvc.unassign_date >= '2016-02-01'
OR mvc.unassign_date IS NULL
AND u.active = 1
AND u.is_deleted != 1)) AS innerData
INNER JOIN
users_department AS departments ON innerData.department_id = departments.id
INNER JOIN
team ON innerData.team_id = team.team_id
INNER JOIN
users AS client_user ON innerData.client_id = client_user.id
INNER JOIN
users AS vp ON innerData.va_id = vp.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment