I wanted to know the average number of orders paid per ticket in the Payment table, how would I do it?
To calculate the average in MySQL
we use the function AVG
.
In this way you filter the form of payment by the code, which in this case is 6
:
SELECT a.'Tipo', AVG(b.'ValorPagar') AS media FROM FormaPagamento a
INNER JOIN Pagamento b ON a.'CodigoPagForm' = b.'CodigoFormaPagamento'
WHERE a.'CodigoPagForm' = 6
GROUP BY a.'Tipo';
And this way you filter by type:
SELECT a.'Tipo', AVG(b.'ValorPagar') AS media FROM FormaPagamento a
INNER JOIN Pagamento b ON a.'CodigoPagForm' = b.'CodigoFormaPagamento'
WHERE a.'Tipo' = 'Boleto'
GROUP BY a.'Tipo';
See more about the AVG
function here .
Calculate the mean:
Select codigo, AVG(valor) FROM Tabela
Calculate the mean with target values:
Select codigo, AVG(DISTINCT valor) FROM Tabela
Calculate the mean and sort the result by the identifier (id):
Select codigo, AVG(valor) from Tabela GROUP BY codigo
Calculates the mean of the target values and sorts the result by the identifier (id):
Select codigo, AVG(DISTINCT valor) from Tabela GROUP BY codigo
Example by W3sscoming .
Documentation MySql .