SQL Server數據庫動態交叉表的參考示例:
--建立測試環境
以下爲引用的內容: set nocount on create table test(model varchar(20),date int ,qty int) insert into test select 'a','8','10' insert into test select 'a','10','50' insert into test select 'b','8','100' insert into test select 'b','9','200' insert into test select 'b','10','100' insert into test select 'c','10','200' insert into test select 'd','10','300' insert into test select 'e','11','250' insert into test select 'e','12','100' insert into test select 'f','12','150' go --測試 declare @sql varchar(8000) set @sql='select model,' select @sql=@sql+'sum(case when date='''+cast(date as varchar(10))+''' then qty else 0 end) ['+cast(date as varchar(10))+'],' from (select distinct top 100 percent date from test order by date)a set @sql =left(@sql,len(@sql)-1)+' from test group by model' exec(@sql) --刪除測試環境 /**//* |