I start by explaining my problem, my database is an inventory and as such I have products and categories, so each product belongs to a different category. I want to make a query where I can count the number of products that each category has and in turn select only those categories that have less than 3 products in it. I have managed to count the number of products by category as follows:
SELECT Categorias.Nombre_Categoria, count(Productos.Nombre_Producto) as 'Cantidad' FROM Categorias INNER JOIN Productos ON Categorias.ID_Categoria = Productos.ID_Categoria group by Categorias.Nombre_Categoria order by Categorias.Nombre_Categoria
The problem is when I want to filter the results with the condition that only categories with less than 3 products are shown. I've tried it in the following way by adding WHERE :
SELECT Categorias.Nombre_Categoria, count(Productos.Nombre_Producto) as 'Cantidad' FROM Categorias INNER JOIN Productos ON Categorias.ID_Categoria = Productos.ID_Categoria where 'Cantidad' < 3 group by Categorias.Nombre_Categoria order by Categorias.Nombre_Categoria
but immediately returns the following error:
Conversion failed when converting the varchar value 'Amount' to data type int.
I want to know how I can convert the values of the column 'Quantity' into integers to filter the results of the query, or how to perform the query that counts the number of products that each category has and select only those with less than 3 products.
I hope you can help me.