sqlserver一个动态交叉表的范例

2016-02-19 16:02 36 1 收藏

下面请跟着图老师小编一起来了解下sqlserver一个动态交叉表的范例,精心挑选的内容希望大家喜欢,不要忘记点个赞哦!

【 tulaoshi.com - 编程语言 】

  社区问的人太多了,保存一个备用

  --建立测试环境

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)

  --删除测试环境

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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
*/

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1610932.html

延伸阅读
WPS文字怎样制作一个刻度表   最终效果图: 1、打开WPS文字,点击绘图工具栏中的自选图形→流程图→流程图:或者,在空白处画出图形,大小自定。 2、右键点击图形,选择设置对象格式。在颜色与线条选项卡中,填充色选为无色,线条粗细选择1.5. 3、切换到版式选项卡,选择浮于文字上方,勾选其他方式...
if not exists (select * from dbo.sysobjects where id = object_id(N'[IndexTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) create table IndexTable(Ex char(20), num integer) go create procedure SetIndex @Ex char(20),@result char(30) output,@Fmt integer as   declare @num...
标签: Web开发
*{ margin:0px;padding:0px;font-size:12px; } input{ width:100px;height:20px;border:1px solid #ccc; } 姓名: 密码: [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 比较简单,自己闲着无聊写的,很久没有写过代码了。。。。 
秋季很容易长胖,一不小心秋膘就贴上身了。 一个月减肥计划表 帮助你拒绝肥胖然后做个瘦美人。如果不想还没入冬就已经变成大胖子,你就必须要制定一个月减肥计划表了,从饮食、运动、护理这三个方面着手,将脂肪“扼杀在摇篮里”。下面图老师小编小编和大家分享一份一个月减肥计划表。 一个月减肥计划表 一个月减肥计划之饮食...
标签: Web开发
前面说的显示当前时间日期是静态的,我们这里来实现一个动态的时钟。 在head和/head之间插入下列代码: script //定义时钟显示的函数 function displayTime() { var today = new Date(); // 定义日期对象 var hours = today.getHours(); var minutes = today.getMinutes(); var seconds = today.getSeconds(); // 从日期对象中中获得时间信...

经验教程

269

收藏

89
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部