Change column header with formula

Asked

Viewed 80 times

1

I searched infinity on the internet and did not find, hope someone can help me, I have the following table:

inserir a descrição da imagem aqui

the numbers -1, -2, -3... represent the months, that is, past month(-1), past tense(-2)... and so on, what I need is some formula to inform the month instead of these numbers in this table. The command to get to this table is as follows:

SELECT 
(SELECT SUM(quantidade) AS Quantidade
FROM genius.itens_compra
LEFT JOIN genius.compras ON genius.compras.Id = Id_compra
WHERE `Id_produto` = 489 AND EXTRACT(YEAR_MONTH FROM `data_compra`) BETWEEN EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL 10 MONTH) AND EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL 10 MONTH)
)AS '-10';

this is repeated from -10 to -1, I just need to exchange the AS part '-10' on the last line for some command that gives a date

  • You want to change the -1 for example July?

  • Wouldn’t it be wiser to do it vertically? Month column and Quantity column? There it would be enough to group by month and year, with a much shorter and reusable query.

  • yes, it would be much simpler, but what happens is that I can’t change the database :/

  • exactly Cleidimar, but I need it to change according to the current month, so I need a formula

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.