Первое: есть серьезная проблема с вашими данными - поскольку нечего связывать то, что вы определили как отдельные строки, как таковые, кроме упорядочения набора результатов. У вас должен быть столбец с именем «item», который связывает их вместе.
При этом я наложил порядок в следующем примере SQL:
;WITH CTE_TestData as (
select AnalysisID = 44, RefID=27, RefColumn = 'Reporting_Currency', Value='EUR'
union all select 44, 27, 'Reporting_Currency', 'EUR'
union all select 44, 27, 'Reporting_Currency', 'USD'
union all select 44, 27, 'Reporting_Group', '0001'
union all select 44, 27, 'Reporting_Group', '0001'
union all select 44, 27, 'Reporting_Group', '0002'
union all select 44, 27, 'Reporting_Language', 'EN'
union all select 44, 27, 'Reporting_Language', 'EN'
union all select 44, 27, 'Reporting_Language', 'DE'
union all select 65, 27, 'Company_Code', null
union all select 65, 27, 'MANDT', null
union all select 65, 27, 'Reporting_Currency', null
)
select AnalysisID, Reporting_Currency, Reporting_Group, Reporting_Language
from (
select *, rowno = row_number() OVER (PARTITION BY refid, analysisid, RefColumn Order By refid, analysisid, refcolumn)
from
CTE_TestData t
) unpvt
PIVOT (min(value) FOR RefColumn in ([Reporting_Currency], [Reporting_Group], [Reporting_Language])) pvt
Возвращает:
AnalysisID Reporting_Currency Reporting_Group Reporting_Language
44 EUR 0001 EN
44 USD 0002 EN
44 EUR 0001 DE
65 NULL NULL NULL