开发者

Transposing from rows into columns

开发者 https://www.devze.com 2023-01-02 18:22 出处:网络
I have a table like this: NameStateAmount ------------------------------ Pump 1Present开发者_开发问答339

I have a table like this:

Name    State       Amount
------------------------------
Pump 1  Present   开发者_开发问答  339
Pump 1  Optimized   88

I want to transpose it to something like this:

Pump 1  Present     339 Optimized   88

How can I do this with MS SQL 2000? I tried to search for a solution, but couldn't find the most fitting one.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消