最近坛子里问行列转换的很多,简单总结一下。
来源:互联网 发布:数据库理论试题及答案 编辑:程序博客网 时间:2024/04/18 23:51
1 .最基本的行列转换:
文章来源于 http://www.cnblogs.com/zhangzs8896 版权归原作者所有
create table table1(厂家 varchar(10),帐期 varchar(20),num int)
insert into table1
select 'A', '0-30' ,10 union all
select 'A', '31-60' ,20 union all
select 'A', '60以上' ,30 union all
select 'A', '60以上' ,40 union all
select 'B', '0-30' ,40 union all
select 'B', '31-60' ,NULL union all
select 'B', '60以上' ,60
--原始数据格式
select * from table1
--行列转换
--方法1
declare @sql varchar(8000)
select @sql=''
select @sql=@sql+',sum(case 帐期 when '''+帐期+''' then num else 0 end) as '''+帐期+''''
from table1
group by 帐期
exec('select 厂家'+@sql+' from table1 group by 厂家')
--方法2:
select
厂家,
'0-30'=sum(case 帐期 when '0-30' then num else 0 end),
'31-60'=sum(case 帐期 when '30-60' then num else 0 end),
'60以上'=sum(case 帐期 when '60以上' then num else 0 end)
from table1 group by 厂家
--删除测试表
drop table table1
insert into table1
select 'A', '0-30' ,10 union all
select 'A', '31-60' ,20 union all
select 'A', '60以上' ,30 union all
select 'A', '60以上' ,40 union all
select 'B', '0-30' ,40 union all
select 'B', '31-60' ,NULL union all
select 'B', '60以上' ,60
--原始数据格式
select * from table1
--行列转换
--方法1
declare @sql varchar(8000)
select @sql=''
select @sql=@sql+',sum(case 帐期 when '''+帐期+''' then num else 0 end) as '''+帐期+''''
from table1
group by 帐期
exec('select 厂家'+@sql+' from table1 group by 厂家')
--方法2:
select
厂家,
'0-30'=sum(case 帐期 when '0-30' then num else 0 end),
'31-60'=sum(case 帐期 when '30-60' then num else 0 end),
'60以上'=sum(case 帐期 when '60以上' then num else 0 end)
from table1 group by 厂家
--删除测试表
drop table table1
文章来源于 http://www.cnblogs.com/zhangzs8896 版权归原作者所有
- 最近坛子里问行列转换的很多,简单总结一下。
- 简单的行列转换
- 简单的行列转换
- 最近总结了很多C#以及Unity的基础知识,分享一下
- 行列转换的简单统计
- 总结一下最近的生活
- 总结一下最近的体会
- [总结一下]最近的工作
- 整理一个坛子里的笑话...稍微改编一下,就叫做《MM和一个程序员相亲的故事》!
- 整理一个坛子里的笑话...稍微改编一下,就叫做《MM和一个程序员相亲的故事》!
- 整理一个坛子里的笑话...稍微改编一下,就叫做《MM和一个程序员相亲的故事》!
- 整理一个坛子里的笑话...稍微改编一下,就叫做《MM和一个程序员相亲的故事》!
- 一个简单的SQL 行列转换
- Oracle行列转换的简单实现
- Oracle行列转换的思考与总结
- 最近发现自己还不会进制转换呢,学的时候就顺势总结一下
- 简单行列转换探索
- 总结一下最近2个多月写的程序
- 今天下午很郁闷
- 两个CheckBox互动
- Error Correction
- 使用js脚本如何进行客户端验证时出现乱码怎么办?
- 在ASP.NET中值得注意的两个地方
- 最近坛子里问行列转换的很多,简单总结一下。
- Asp.Net下页面之间传值
- 读取存储在MsSql下的文件,配合使用DataGrid的模板列。
- 上传文件到MsSql数据库中
- 创建XML文件的两种方法
- 对XML文件的基本操作
- 一个很有意义的计算题!
- 子网的划分与判定
- 有关树形结构的查询--Sql