La transposition de lignes en colonnes
-
24-10-2019 - |
Question
J'ai une table comme ceci:
Name State Amount
------------------------------
Pump 1 Present 339
Pump 1 Optimized 88
Je veux transposer à quelque chose comme ceci:
Pump 1 Present 339 Optimized 88
Comment puis-je faire avec MS SQL 2000? J'ai essayé de rechercher une solution, mais ne pouvait pas trouver plus d'une installation.
La solution
declare @t table(Name varchar(10), State varchar(10), Amount int)
insert into @t
select 'Pump 1', 'Present', 339 union all
select 'Pump 1', 'Optimized', 88
select name,
max(case when state='Present' then 'Present' end),
max(case when state='Present' then Amount end),
max(case when state='Optimized' then 'Optimized' end),
max(case when state='Optimized' then Amount end)
from @t
group by name
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow