2
What would be the Mysql query to bring the city name with the highest number of sales?
CREATE TABLE `clientes` (
`Codigo` INT(11) NULL DEFAULT NULL,
`Nome` VARCHAR(50) NULL DEFAULT NULL,
`Cidade` VARCHAR(50) NULL DEFAULT NULL
)
CREATE TABLE `vendas` (
`Codigo_Venda` INT(11) NULL DEFAULT NULL,
`CodigoCliente` INT(11) NULL DEFAULT NULL,
`ValorVenda` DOUBLE NULL DEFAULT NULL
)
I tried this query, but it can bring wrong values:
SELECT C.Cidade, MAX(V.ValorVenda)
FROM Clientes C INNER JOIN vendas V ON C.Codigo = V.Codigo_Venda
GROUP BY C.cidade
I tried to put together max(sum(campo))
but mysql is not accepting
MAX returns the highest value of a field. Mysql has not left MAX(SUM()) because it cannot solve nested aggregations. Let’s think a little about your exercise. Abstracting the database, how would you find out which city had MORE SALES? (sales quantity != greater added value of sales)
– Badaro
Another point: your JOIN is comparing customer code with sales code, it should be
CodigoCliente
.– Badaro