Hi, I have a table in SQL Server like this:
Code:
create table t1
([name] varchar(20),Subject varchar(20),Result int)
insert into t1 select 'Jack','Writing',80
insert into t1 select 'Jack','Math',90
insert into t1 select 'Jack','Physics',85
insert into t1 select 'Bob','Writing',85
insert into t1 select 'Bob','Math',92
insert into t1 select 'Bob','Physics',82
insert into t1 select 'Lee','Writing',78
insert into t1 select 'Lee','Physics',87
And I wanna to create a view like this:
Code:
declare @sql varchar(4000)
set @sql = 'select Name'
select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result end) ['+Subject+']'
from (select distinct Subject from t1) as a
select @sql = @sql+' from t1 group by name'
so that I can get the resultset:
Code:
name Writing Math Physics
Jack 80 90 85
Bob 85 92 82
Lee 78 87
As you see, the columns of the result view is uncertain, so.....
i am wondering if I could create a map for such a view? thanks a lot.........