I'm having trouble merging two MySQL queries and getting the right data.
My first request is as follows:
SELECT e.employee_id, e.employee_name, COUNT(s.sale_id) AS employee_sales FROM employees e, sales s, days d WHERE s.sale_id = '$sale_type' AND d.day_year_id = '$year' AND s.sale_day_id = d.day_id AND e.employee_id = s.sale_employee_id GROUP BY e.employee_id
Then, for each employee result, I perform three queries to obtain specific information about each employee:
First, to get the total number of minutes they worked with, I execute the following query. When I try to join this with the first one, I have problems returning all the minutes that they worked on, regardless of whether they made a sale on a specific day:
SELECT SUM(employee_day_end_minute-employee_day_start_minute) AS employee_minutes FROM employee_days ed, days d WHERE ed.employee_days_employee_id = '$employee_id' AND ed.employee_days_day_id = d.day_id AND d.day_year_id = '$year'
Secondly, to get the type of assignment that they performed the most:
SELECT ed.employee_day_position, COUNT(ed.employee_day_position) AS count FROM employee_days ed, days d WHERE ed.employee_days_employee_id = '$employee_id' AND ed.employee_days_day_id = d.day_id AND d.day_year_id = '$year' GROUP BY match_player_position ORDER BY count DESC LIMIT 1
And finally, I get the average weight to multiply the sales values ββbased on the days and days of the days on which they worked:
SELECT (SUM(dw.day_weighting_value)/COUNT(s.day_weighting_value)) AS employee_weigting FROM employee_days ed, day_weightings dw, days d WHERE ed.employee_day_employee_id = '$employee_id' AND ed.employee_day_day_id = d.day_id AND d.day_year_id = '$year' AND dw.day_weighting_day_id = d.day_id AND dw.day_weighting_minute >= ed.employee_day_start_minute AND dw.day_weighting_minute <= ed.employee_day_end_minute`
Can someone give me guidance on whether this is possible at all, and if so, where to start?
Thanks in advance!