databanker
MIS
I am trying to figure out how to increment a column name in a select statement. Basically I want one select statement to run against my 12 monthly balance fields (EOMB). The below code is what I have so far. Any advice is greatly appreciated.
declare @counter int
set @counter = 0
while @counter < 12
begin
set @counter = @counter + 1
SELECT CAST(@counter AS char) AS Month, @counter AS Balance, 'EOMB' AS [Measure Type], [Account Number], Branch
FROM [Import - CY AVG Balances]
end
[Import - CY AVG Balances]
[Account Number]
Branch
[1 EOMB]
[2 EOMB]
[3 EOMB]
[4 EOMB]
[5 EOMB]
[6 EOMB]
[7 EOMB]
[8 EOMB]
[9 EOMB]
[10 EOMB]
[11 EOMB]
[12 EOMB]
declare @counter int
set @counter = 0
while @counter < 12
begin
set @counter = @counter + 1
SELECT CAST(@counter AS char) AS Month, @counter AS Balance, 'EOMB' AS [Measure Type], [Account Number], Branch
FROM [Import - CY AVG Balances]
end
[Import - CY AVG Balances]
[Account Number]
Branch
[1 EOMB]
[2 EOMB]
[3 EOMB]
[4 EOMB]
[5 EOMB]
[6 EOMB]
[7 EOMB]
[8 EOMB]
[9 EOMB]
[10 EOMB]
[11 EOMB]
[12 EOMB]