I am working on a SQL query to display a list of employees who have successfully shipped products, along with the total number of orders taken by each employee. The results should be sorted in descending order based on the total number of orders.
However, the output of my query does not match the expected results. Here are the details:
Sample Output:
EmployeeName Total Orders
Margaret Peacock 3
Michael Suyam 3
Robert King 2
Andrew Fuller 2
Laura Callahan 1
update update 1
Steven Buchanan 1
John Carter 0
My Actual Output:
EmployeeName Total Orders
Margaret Peacock 68
update update 46
Laura Callahan 44
Andrew Fuller 29
Robert King 27
Michael Suyam 27
Here is the query I am using:
SELECT
CONCAT(e.FirstName, e.LastName) AS EmployeeName,
COUNT(o.OrderID) AS [Total Orders]
FROM
Employees e
LEFT JOIN
Orders o ON e.EmployeeID = o.EmployeeID
WHERE
o.ShippedDate IS NOT NULL
GROUP BY
e.FirstName, e.LastName
ORDER BY
[Total Orders] DESC;
Here is the query I am using:
SELECT
CONCAT(e.FirstName, e.LastName) AS EmployeeName,
COUNT(o.OrderID) AS [Total Orders]
FROM
Employees e
JOIN
Orders o ON e.EmployeeID = o.EmployeeID
WHERE
o.ShippedDate IS NOT NULL
GROUP BY
e.FirstName, e.LastName
ORDER BY
[Total Orders] DESC;
Despite my efforts, I am unable to get the expected results. Can someone help me identify what might be going wrong with my query or suggest any improvements?
This Is My Question:
Write a SQL query to display the employee’s list who are all shipped the products successfully for the required date of the customers. i. Concatenated value of the employee’s ‘FirstName’ and ‘LastName’ column value under the title of ‘EmployeeNme’ ii. Total number of orders taken by each employee under the title of ‘Total Orders’ NOTE: The final output should displayed descending based on the ‘Total Orders’ value.
Here is the query I am using:
SELECT
CONCAT(e.FirstName, e.LastName) AS EmployeeName,
COUNT(o.OrderID) AS [Total Orders]
FROM
Employees e
LEFT JOIN
Orders o ON e.EmployeeID = o.EmployeeID
WHERE
o.ShippedDate IS NOT NULL
GROUP BY
e.FirstName, e.LastName
ORDER BY
[Total Orders] DESC;
This is the ouput i got:
My Actual Output:
EmployeeName Total Orders
Margaret Peacock 68
update update 46
Laura Callahan 44
Andrew Fuller 29
Robert King 27
Michael Suyam 27
But i need output like this:
EmployeeName Total Orders
Margaret Peacock 3
Michael Suyam 3
Robert King 2
Andrew Fuller 2
Laura Callahan 1
update update 1
Steven Buchanan 1
John Carter 0
Sushmi Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.