Get data from a created column

1

Good night, I have a table with 4 fields and through them I make some calculations and create a new field to show these same results ... The problem is that later I want to go get those results and I do not know how to redeem them. / p>

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

    
asked by anonymous 01.07.2017 / 03:10

1 answer

3

One way to solve the problem would be to get your query and play inside a subquery, and then select these fields you want "out". It 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
    
01.07.2017 / 05:30