CREATE TABLE [Test] (<br> <br> [id] [int] IDENTITY (1, 1) NOT NULL ,<br> <br> [name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,<br> <br> [subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,<br> <br> [Source] [numeric](18, 0) NULL<br> <br> ) ON [PRIMARY]<br> <br> GO<br> <br> INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)<br> <br> INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)<br> <br> INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)<br> <br> INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)<br> <br> INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)<br> <br> INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)<br> <br> INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)<br> <br> Go<br> <br> <br><br><br> 交叉表语句的实现:<br> <br> 用于:交叉表的列数是确定的<br> <br> select name,sum(case subject when '数学' then source else 0 end) as '数学',<br> <br> sum(case subject when '英语' then source else 0 end) as '英语',<br> <br> sum(case subject when '语文' then source else 0 end) as '语文'<br> <br> from test<br> <br> group by name