Home > other >  SQL Oracle Query for counting number of orders per city
SQL Oracle Query for counting number of orders per city

Time:01-15

I have a table called "Orders" and I need to get a list with only the name of the cities that have had more than 30 orders.

the columns:

OrderID, CustomerID, EmployeeID, TerritoryID,
Orderdate, Requireddate, Shippeddate, Shipvia,
Freight, Shipname, Shipaddress, Shipcity, Shipregion,
Shippostalcode, Shipcountry

I have managed to get it to tell me the number of orders by city with the 'Shippostalcode' but I don't know how to tell it to show me only those with more than 30 orders

The code I was trying was this:

SELECT shipcity,
        COUNT(shippostalcode) AS order_numbers
        FROM orders
        where order_numbers>30
   GROUP BY shipcity
   ORDER BY order_numbers DESC;

CodePudding user response:

It is not a WHERE, but HAVING clause:

SELECT shipcity,
       COUNT(*) AS order_numbers
FROM orders
GROUP BY shipcity
HAVING COUNT(*) > 30
ORDER BY order_numbers DESC;
  • Related