Testing pivot

0

Please login or register to vote for this query.

(click on this box to dismiss)

Economics

Q&A for those who study, teach, research and apply economics and econometrics

create table #foo (
    id int not null,
    name nvarchar(10) not null,
    value nvarchar(10)
    , x nvarchar(10)
);


insert into #foo values (1, 'a', 'v1', 's');
insert into #foo values (1, 'a', 'v2', 't');
insert into #foo values (1, 'b', 'v3', 'u');
insert into #foo values (2, 'b', 'v3', 'v');
/*
insert into #foo values (1, 'a', 'v1');
insert into #foo values (1, 'a', 'v2');
insert into #foo values (1, 'b', 'v3');
insert into #foo values (2, 'b', 'v3');
*/
-- select count(*) from #foo;

select id, a, b
from 
  (select id, name, value, x from #foo) as src
pivot
( max(src.value)
  for src.name 
    in (a, b) 
) as res;

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results