Retrieve data from a created column

Asked

Viewed 112 times

1

Good evening, I have a table with 4 fields and through them I do some calculations and create a new field to show these same results... The problem is that then I want to get those results and I don’t know how I can rescue them.

declare @mesAtual as int = (6)

        select  mes, 
                valor, 
                periodicidade , 
                cast(valor / periodicidade as decimal(18,2)) as ValorMes,
                case when @mesAtual > mes then @mesAtual - mes
                when @mesAtual < mes then (periodicidade - (mes - @mesAtual)) + 1
                when @mesAtual = mes then 0 end as Diferenca

        from tblPrevista

inserir a descrição da imagem aqui

  • I suggest that you paste in the question the code of your SQL query beyond the image, so whoever helps you can copy the query more easily.

  • 1

    You’re absolutely right, thank you for the suggestion. @Ana Carolina Manzan

1 answer

3


One way to solve the problem would be to take this query of yours and play inside a subquery, and then select these fields that you want "on the outside". Would look like this:

declare @mesAtual as int = (6)

SELECT  *,
        ValorMes * Diferenca as Final
FROM  
(SELECT mes, 
        valor,
        periodicidade,
        cast(valor/periodicidade as decimal(18,2)) as ValorMes,
        CASE 
        WHEN @mesAtual > mes then @mesAtual - mes
        WHEN @mesAtual < mes then (periodicidade - (mes - @mesAtual)) + 1
        WHEN @mesAtual = mes then 0 end as Diferenca
 FROM tblPrevista) as tblPrevistaCalculos
  • 1

    Thank you very much, this reply helped me a lot.

Browser other questions tagged

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