El HAVING se agrego a SQL para solucionar el problema de WHERE en sitio onde no puede ser agregado
Sintaxis de HAVING
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
...
Ejemplos de HAVING
La seguiente instrucion enumera la cantidad de clientes en cada pais onde el numero sea mayor que 5
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
Esta instrucion es igual a la anterior, pero ordena los paises de mayor a menor
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5 ORDER BY COUNT(CustomerID) DESC;
Esta instrucion enumera los empleados con mas de 10 pedidos
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID) GROUP BY LastName HAVING COUNT(Orders.OrderID) > 10;
Esta instrucion enumera los empleados Julio y Manolo si registraran mas de 25 pedidos
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID WHERE LastName = 'Julio' OR LastName = 'Manolo' GROUP BY LastName HAVING COUNT(Orders.OrderID) > 25;
0 Comentarios
Si desea contactar comigo, lo puede hacer atravez deste formulario gracias