SQL Server数据库动态交叉表的参考示例-钱玉炜
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> ADP及SQL SERVER


SQL Server数据库动态交叉表的参考示例

发表时间:2008/8/14 8:08:29 评论(0) 浏览(5725)  评论 | 加入收藏 | 复制
   
摘 要:SQL Server数据库动态交叉表的参考示例
正 文:


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)

--删除测试环境
drop table test
 set nocount off

/**//*
model                8           9           10          11          12
-------------------- ----------- ----------- ----------- ----------- -----------
a                    10          0           50          0           0
b                    100         200         100         0           0
c                    0           0           200         0           0
d                    0           0           300         0           0
e                    0           0           0           250         100
f                    0           0           0           0           150
*/
 


Access软件网交流QQ群(群号:198465573)
 
 相关文章
交叉表还原为数据列表  【叶海峰  2012/9/17】
交叉表查询实现生成的表中null变0  【蒋元根  2013/3/7】
多个交叉表做子窗体示例(动态图表)  【林岚  2013/7/3】
如何利用动态交叉表生成子窗体子报表  【蟹仔  2013/7/4】
【Access示例】利用交叉表实现行列的转换,使列变成yyyymm...  【缪炜  2013/7/5】
常见问答
技术分类
相关资源
文章搜索
关于作者

钱玉炜

文章分类

文章存档

友情链接