xxxxxxxxxx
DECLARE @colsUnpivot AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @colsUnpivot
= stuff((select ','+quotename(C.column_name)
from information_schema.columns as C
where C.table_name = 'yourtable' and
C.column_name like 'Indicator%'
for xml path('')), 1, 1, '')
set @query
= 'select id, entityId,
indicatorname,
indicatorvalue
from yourtable
unpivot
(
indicatorvalue
for indicatorname in ('+ @colsunpivot +')
) u'
exec sp_executesql @query;
xxxxxxxxxx
SELECT *
FROM
(
SELECT column1, column2 -- Specify the columns you want to pivot
FROM your_table
) AS SourceTable
PIVOT
(
MAX(column2) -- Apply an aggregation function (e.g., MAX, MIN, SUM) to the column you want to pivot
FOR column1 IN ([Value1], [Value2], [Value3]) -- Provide the distinct values in column1 to become columns
) AS PivotTable;