You can calculate the average before and assign to a variable:
DECLARE @media numeric(15, 2);
SELECT @media = AVG(l.price)
FROM livros l;
SELECT l.*
FROM livros l
WHERE l.price > @media;
Or perform a CROSS JOIN
with the calculated value:
SELECT l.*
FROM livros l
CROSS JOIN (SELECT AVG(l2.price) AS media
FROM livros l2) m
WHERE l.price > m.media;
A third way is to calculate the average with the WITH
:
WITH media AS (
SELECT AVG(l2.price) AS media
FROM livros l2
)
SELECT l.*
FROM livros l
WHERE l.price > (SELECT m.media
FROM media m);
What is the meaning of this (15,2)?
– M.Amaral
@M.Amaral the
15
is the size of the whole part of the number and the2
is the maximum of decimal places that will be stored– Sorack
The person responsible for
downvote
can explain why the answer would be wrong? I don’t understand thedownvote
without explanation and reason.– Sorack