'How do I fix the group by statement
here is the question
Using Sales.SalesTerritory and Sales.SalesOrderHeader, find out the territories that have more than 1000 sales orders. Please return the TerritoryID, the Territory Name, and the total number of sales orders. Order by the total number of sales orders.
here is my code
Any pointers would be greatly appreciated thank you.
Solution 1:[1]
I don't have a server to try that out, but you might want to try the following: Move the WHERE part below the GROUP BY part and change WHERE to HAVING
SELECT
SalesOrderHeader.TerritoryID, Name, SUM(COUNT(SalesOrderNumber)) AS 'Total Sales Orders'
FROM
Sales.SalesTerritory
INNER JOIN ...
GROUP BY
SalesOrderHeader.TerritoryID
HAVING
SUM(COUNT(SalesOrderNumber)) > 1000;
I am not sure if you can use the alias, but maybe even that is possible.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
