Im learning SQL and i did this code (ignore words in portuguese):
SELECT EMBALAGEM,
CASE
WHEN PRECO_DE_LISTA >= 12 THEN 'PRODUTO_CARO'
WHEN PRECO_DE_LISTA >= 7 AND PRECO_DE_LISTA < 12 THEN 'PRODUTO_EM_CONTA'
ELSE 'PRODUTO_BARATO'
END AS STATUS_PRECO, AVG(PRECO_DE_LISTA) AS PRECO_MEDIO
FROM tabela_de_produtos
GROUP BY EMBALAGEM,
CASE
WHEN PRECO_DE_LISTA >= 12 THEN 'PRODUTO_CARO'
WHEN PRECO_DE_LISTA >= 7 AND PRECO_DE_LISTA < 12 THEN 'PRODUTO_EM_CONTA'
ELSE 'PRODUTO_BARATO'
END;
https://i.stack.imgur.com/TedKP.png
After that, I've tried to take the average from the column created with CASE ("PRECO_MEDIO") using:
SELECT AVG(PRECO_MEDIO) AS AVERAGE FROM (the previous code);
But didn't work, I've got this error: Every derived table must have its own alias. How can i do the mean of that column?