2
I need to make a select where a condition is checked.
Ex: If the value of a given field is < 0 assign the sum of that field to column "PerdaGerada
" otherwise assign the column "LucroGerado
".
Something as shown below:
SELECT TOP 10
TBL.PRODUTO PRODUTO,
COUNT(TBL.ID) AS QTD,
SE TBL.VALORGANHO < = ENTAO SOMA DE VALOR GANHO VAI PRA COLUNA "PERDAGERADA"
SE TBL.VALORGANHO > = ENTAO SOMA DE VALOR GANHO VAI PRA COLUNA "LUCROGERADO"
FROM TABELAVENDAS TBL
WHERE TBL.DATAEMISSAO BETWEEN '20150901' and '20150930'
GROUP BY TBL.PRODUTO
ORDER BY 1 DESC
Tip: use
CASE
– DH.
I did so ... CASE WHEN(ACC.VALUATION < 0 SUM(ACC.VALUATION GAIN) AS PERDAGER ELSE SUM(ACC.VALUATION GAIN) AS ECONOMIAGER END , but it didn’t work out not
– Alvaro Vieira
trial
CASE WHEN x THEN y WHEN w THEN z END AS "alias"
Unfortunately there is no way to make a dynamic alias, as far as I know.– DH.
I couldn’t do it this way ...
– Alvaro Vieira