I am adding a column of my query:
SELECT SEGMENTO, ESTATUS, CONCEPTO, SUM([ENE-20]) AS [ENE-20]
FROM Tabla
GROUP BY CONCEPTO, ESTATUS, SEGMENTO
I need to do the same process only the sum has to be a variable, this is what I have:
DECLARE @TEST NVARCHAR(10)
SET @TEST = '[ENE-20]'
SELECT SEGMENTO, ESTATUS, CONCEPTO, SUM(@TEST) AS [ENE-20]
FROM Tabla
GROUP BY CONCEPTO, ESTATUS, SEGMENTO
But it shows me the following error:
Operand data type nvarchar is invalid for sum operator.
You have to do a dynamic sql.
This should work for you:
I leave you this page where a little more comes and it helped me a lot a long time ago https://ing-sistemas.com/2017/03/24/sql-dinamico-en-transact-sql/