I am having problems with the following sub query calculation. What I would like to achieve is grand totals for all records, sub-totals by employee, then subtotals by shipping method. I can get the first two to work as expected, but the last subtotal by shipping method (TOT_FREIGHT) is not working correctly. I am using the northwind database based on the Orders table.
This is the output I would like to achieve:
Employee Ship Via Freight TOT_FREIGHT TOT_EMP TOT_ALL
Buchanan Fed Ex $12 $25 $175 $1500
Buchanan Fec Ex $13 $25 $175 $1500
This is the SQL:
SELECT Orders.EmployeeID, Orders.ShipVia, Orders.Freight, (SELECT SUM([Orders.Freight])
FROM Orders As T2
WHERE T2.[EmployeeID] = Orders.[EmployeeID] AND T2.[ShipVia] = Orders.[ShipVia]) AS TOT_FREIGHT,
(SELECT SUM([Freight]) FROM Orders AS T2 WHERE T2.[EmployeeID] = orders.[EmployeeID]) AS TOT_EMP,
(SELECT SUM([Freight]) FROM Orders) AS TOT_ALL
FROM Orders
GROUP BY Orders.EmployeeID, Orders.ShipVia, Orders.Freight
HAVING (((Orders.EmployeeID)=5) AND ((Orders.ShipVia)=3));
Thanks.
This is the output I would like to achieve:
Employee Ship Via Freight TOT_FREIGHT TOT_EMP TOT_ALL
Buchanan Fed Ex $12 $25 $175 $1500
Buchanan Fec Ex $13 $25 $175 $1500
This is the SQL:
SELECT Orders.EmployeeID, Orders.ShipVia, Orders.Freight, (SELECT SUM([Orders.Freight])
FROM Orders As T2
WHERE T2.[EmployeeID] = Orders.[EmployeeID] AND T2.[ShipVia] = Orders.[ShipVia]) AS TOT_FREIGHT,
(SELECT SUM([Freight]) FROM Orders AS T2 WHERE T2.[EmployeeID] = orders.[EmployeeID]) AS TOT_EMP,
(SELECT SUM([Freight]) FROM Orders) AS TOT_ALL
FROM Orders
GROUP BY Orders.EmployeeID, Orders.ShipVia, Orders.Freight
HAVING (((Orders.EmployeeID)=5) AND ((Orders.ShipVia)=3));
Thanks.