I am trying to get this output using a SQL statement and the NORTHWIND database:
Employee Name:Nancy Davolio
Number of Sales:345
Total Sales:192107.60
Employee Name:Andrew Fuller
Number of Sales:241
Total Sales:166537.75
Employee Name:Janet Leverling
Number of Sales:321
Total Sales:202812.84
Employee Name:Margaret Peacock
Number of Sales:420
Total Sales:232890.85
Employee Name:Steven Buchanan
Number of Sales:117
Total Sales:68792.28
...and 4 more entries
When I use this statement:
USE Northwind
DECLARE @EmployeeName VARCHAR(40),
@NumberOfSales INT,
@TotalSales DECIMAL(10,2),
@Counter TINYINT = 1,
@NumEmployees INT = IDENT_CURRENT('dbo.Employees');
WHILE @Counter < @NumEmployees
BEGIN
--SELECT @EmployeeName = E.FirstName+' '+E.LastName
--SELECT @NumberOfSales = count(od.OrderID)
SELECT @TotalSales = SUM(unitprice * quantity * (1 - Discount))
FROM Employees E
JOIN Orders AS O ON O.EmployeeID = E.EmployeeID
JOIN [Order Details] AS OD ON OD.OrderID = O.OrderID
WHERE E.EmployeeID = @Counter
PRINT 'Employee Name: '--+ @EmployeeName;
PRINT 'Number of Sales: '--+ LTRIM(STR(@NumberOfSales));
PRINT 'Total Sales: '+CONVERT(varchar(10),@TotalSales);
PRINT '';
SET @Counter += 1;
END
I can get each select to work singly but I cannot figure out the syntax to get a single SELECT
statement to do all the work. I should also be able to do this with three SET
statements but I've not been able to figure that out either. Pointers to both possibilities would be awesome.
Here's that actual step verbiage: "Within the loop, use a SELECT statement to retrieve the first and last name of each employee, the number of orders handled by each employee and the total sales amount for each employee (you are processing each employee one by one). You will need to join multiple tables together and use aggregate functions to get a count and a total. Assign the concatenated full name, number of sales and total sales amount to the appropriate variables."
Output should be in Messages tab, no table or format other than the expected output listed above.