Skip to content

Instantly share code, notes, and snippets.

@said-and-done
Created March 28, 2014 11:43
Show Gist options
  • Save said-and-done/e3e7a68fc2c84068d94a to your computer and use it in GitHub Desktop.
Save said-and-done/e3e7a68fc2c84068d94a to your computer and use it in GitHub Desktop.
SELECT salesperson_id,
Sum(sold / salesperson_count) AS amount
FROM project_items
JOIN(SELECT allocation.project_item_id,
sold,
salesperson_id
FROM project_item_sellers
JOIN(SELECT project_item_id,
Sum(amount) AS sold
FROM project_item_allocations AS allocation
WHERE Year(allocation.created) = 2014
AND Month(allocation.created) = 03
GROUP BY project_item_id
HAVING sold <> 0
) allocation
ON allocation.project_item_id =
project_item_sellers.project_item_id
GROUP BY allocation.project_item_id, salesperson_id
) t2
ON t2.project_item_id = project_items.id
GROUP BY salesperson_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment