I have been trying to get the name of employees first name for all sales of the sales person with the most sales.
I have tried these 2 solutions but none of them seems to work.
(P.S. I will provide more information if this is not enough)
SELECT s.date, st.item, st.price, st.category, e.first_name as firstname, COUNT(*) AS allsales
FROM sales s INNER JOIN employees e ON s.employee = e.id
INNER JOIN stock_items st ON s.item = st.id
GROUP BY firstname ORDER BY s.date DESC LIMIT 1;
SELECT s.date, st.item, st.price, st.category, e.first_name as firstname, COUNT(*) AS allsales
FROM sales s INNER JOIN employees e ON s.employee = e.id
INNER JOIN (SELECT COUNT(*) AS allsales, e.first_name AS firstname,
FROM sales s INNER JOIN employees e ON s.employee = e.id
GROUP BY firstname ORDER BY allsales DESC LIMIT 1) as t ON e.first_name = t.firstname;
3
Answers
This query first finds the employee with the most sales, and then selects all the sales of that employee
Assuming each sale results in exactly one record in
sales
:This returns the employee id, firstname and the amount of sales.
when using aggregation functions like count(*) you must include all the non aggregated selected columns in the group by