У меня есть сводный SQL-запрос, который выведет таблицу. Но мне нужно отформатировать имена динамических столбцов и результаты.
Это запрос:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
DECLARE @yearV varchar(max) = '2019'
DECLARE @idclienteV varchar(max) = 1
select @cols = STUFF(
(
SELECT ',' + QUOTENAME([Y].[FechaPpto])
FROM [dbo].[Ingresos_Ppto] [Y]
WHERE
([Y].[IdCliente] = @idclienteV) AND (DATEPART(YEAR, [Y].[FechaPpto]) = @yearV)
GROUP BY [Y].[FechaPpto]
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query = N'DECLARE @year int = ' + @yearV + ' DECLARE @idc int = ' + @idclienteV + ' SELECT Rubro, ' + @cols + N' FROM
(
SELECT [Y].[Rubro], [Y].[Valor], [Y].[FechaPpto]
FROM [Ingresos_Ppto] [Y]
WHERE
([Y].[IdCliente] = @idc)
AND
(DATEPART(YEAR, [Y].[FechaPpto]) = @year)
) X
PIVOT
(
max(Valor)
FOR FechaPpto in (' + @cols + N')
) p'
EXEC sp_executesql @query;
И вот эта таблица, которую он принесет:
Rubro | Jan 1 2019 12:00 AM | Feb 1 2019 12:00 AM | Mar 1 2019 12:00 AM
+--------------------------+---------------------+---------------------+----------------------
Branding | 3280937.00 | 4912017.00 | 2404802.00
+--------------------------+---------------------+---------------------+----------------------
Mercadeo y publicidad | 3372619.00 | 7423175.00 | 8736550.00
+--------------------------+---------------------+---------------------+----------------------
Portal web WP | 9148489.00 | 9172295.00 | 4643597.00
+--------------------------+---------------------+---------------------+----------------------
Portal web WP + ecommerce | 3785304.00 | 9140700.00 | 7425106.00
+--------------------------+---------------------+---------------------+----------------------
Renting tecnológico | 7223406.00 | 7298693.00 | 8768783.00
+--------------------------+---------------------+---------------------+----------------------
Так что мне нужно изменить имя столбца даты и результат, чтобы получить это:
Rubro | Januray 2019 | February 2019 | March 2019
+--------------------------+---------------------+---------------------+----------------------
Branding | $3,280,937.00 | $4,912,017.00 | $2,404,802.00
+--------------------------+---------------------+---------------------+----------------------
Mercadeo y publicidad | $3,372,619.00 | $7,423,175.00 | $8,736,550.00
+--------------------------+---------------------+---------------------+----------------------
Portal web WP | $9,148,489.00 | $9,172,295.00 | $4,643,597.00
+--------------------------+---------------------+---------------------+----------------------
Portal web WP + ecommerce | $3,785,304.00 | $9,140,700.00 | $7,425,106.00
+--------------------------+---------------------+---------------------+----------------------
Renting tecnológico | $7,223,406.00 | $7,298,693.00 | $8,768,783.00
+--------------------------+---------------------+---------------------+----------------------
Пока это то, что я пробовал:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
DECLARE @yearV varchar(max) = '2019'
DECLARE @idclienteV varchar(max) = 1
select @cols = STUFF(
(
SELECT ',' + QUOTENAME(FORMAT(dateadd(month, 0, [Y].[FechaPpto]), 'MMM', 'es-co'))
FROM [dbo].[Ingresos_Ppto] [Y]
WHERE
([Y].[IdCliente] = @idclienteV) AND (DATEPART(YEAR, [Y].[FechaPpto]) = @yearV)
GROUP BY FORMAT(dateadd(month, 0, [Y].[FechaPpto]), 'MMM', 'es-co')
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'),1,1,'')
set @query = N'DECLARE @year int = ' + @yearV + ' DECLARE @idc int = ' + @idclienteV + ' SELECT Rubro, ' + @cols + N' FROM
(
SELECT [Y].[Rubro], [Y].[Valor], [Y].[FechaPpto]
FROM [Ingresos_Ppto] [Y]
WHERE
([Y].[IdCliente] = @idc)
AND
(DATEPART(YEAR, [Y].[FechaPpto]) = @year)
) X
PIVOT
(
max(Valor)
FOR FORMAT(dateadd(month, 0, [Y].[FechaPpto]), 'MMM', 'es-co') in (' + @cols + N')
) p'
EXEC sp_executesql @query;
Но яполучаю несколько синтаксических ошибок.
Как мне отформатировать этот результат?