很簡單的一個東西
交叉表實例
建表
在查詢分析器裡運行
CREATE TABLE [Test] (
[id] [int] IDENTITY (
[name] [nvarchar] (
[subject] [nvarchar] (
[Source] [numeric](
) ON [PRIMARY]
GO
INSERT INTO [test] ([name]
INSERT INTO [test] ([name]
INSERT INTO [test] ([name]
INSERT INTO [test] ([name]
INSERT INTO [test] ([name]
INSERT INTO [test] ([name]
INSERT INTO [test] ([name]
Go
交叉表語句的實現
用於
select name
sum(case subject when
sum(case subject when
from test
group by name
declare @sql varchar(
set @sql =
select @sql = @sql +
then source else
from (select distinct subject from test) as a
select @sql = left(@sql
exec(@sql)
go
運行結果
From:http://tw.wingwit.com/Article/program/SQLServer/201311/22229.html