仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 601|回复: 7
打印 上一主题 下一主题

[学习教程] MYSQL编程:sqlserver 行列互转完成小结

[复制链接]
金色的骷髅 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:15:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
甚至一个有经验的Windows管理者也可以轻松部署并开始学习它,而你不需投入一分钱来了解这个数据库。复制代码代码以下:
--行列互转
/******************************************************************************************************************************************************
以先生成就为例子,对照抽象易懂

收拾人:中国风(Roy)

日期:2008.06.06
******************************************************************************************************************************************************/

--1、行互列
-->-->(Roy)天生y

ifnotobject_id(Class)isnull
droptableClass
Go
CreatetableClass([Student]nvarchar(2),[Course]nvarchar(2),[Score]int)
InsertClass
selectN张三,N语文,78unionall
selectN张三,N数学,87unionall
selectN张三,N英语,82unionall
selectN张三,N物理,90unionall
selectN李四,N语文,65unionall
selectN李四,N数学,77unionall
selectN李四,N英语,65unionall
selectN李四,N物理,85
Go
--2000办法:
静态:

declare@snvarchar(4000)
set@s=
Select@s=@s+,+quotename([Course])+=max(casewhen[Course]=+quotename([Course],)+then[Score]else0end)
fromClassgroupby[Course]
exec(select[Student]+@s+fromClassgroupby[Student])


天生静态:

select
[Student],
[数学]=max(casewhen[Course]=数学then[Score]else0end),
[物理]=max(casewhen[Course]=物理then[Score]else0end),
[英语]=max(casewhen[Course]=英语then[Score]else0end),
[语文]=max(casewhen[Course]=语文then[Score]else0end)
from
Class
groupby[Student]

GO
静态:

declare@snvarchar(4000)
Select@s=isnull(@s+,,)+quotename([Course])fromClassgroupby[Course]
exec(select*fromClasspivot(max([Score])for[Course]in(+@s+))b)

天生静态:
select*
from
Class
pivot
(max([Score])for[Course]in([数学],[物理],[英语],[语文]))b

天生格局:
/*
Student数学物理英语语文
---------------------------------------------------
李四77856565
张三87908278

(2行受影响)
*/

------------------------------------------------------------------------------------------
go
--加上总成就(学科均匀分)

--2000办法:
静态:

declare@snvarchar(4000)
set@s=
Select@s=@s+,+quotename([Course])+=max(casewhen[Course]=+quotename([Course],)+then[Score]else0end)
fromClassgroupby[Course]
exec(select[Student]+@s+,[总成就]=sum([Score])fromClassgroupby[Student])--加多一列(学科均匀分用avg([Score]))

天生静态:

select
[Student],
[数学]=max(casewhen[Course]=数学then[Score]else0end),
[物理]=max(casewhen[Course]=物理then[Score]else0end),
[英语]=max(casewhen[Course]=英语then[Score]else0end),
[语文]=max(casewhen[Course]=语文then[Score]else0end),
[总成就]=sum([Score])--加多一列(学科均匀分用avg([Score]))
from
Class
groupby[Student]

go

--2005办法:

静态:

declare@snvarchar(4000)
Select@s=isnull(@s+,,)+quotename([Course])fromClassgroupby[Course]--isnull(@s+,,)往失落字符串@s中第一个逗号
exec(select[Student],+@s+,[总成就]from(select*,[总成就]=sum([Score])over(partitionby[Student])fromClass)a
pivot(max([Score])for[Course]in(+@s+))b)

天生静态:

select
[Student],[数学],[物理],[英语],[语文],[总成就]
from
(select*,[总成就]=sum([Score])over(partitionby[Student])fromClass)a--均匀分时用avg([Score])
pivot
(max([Score])for[Course]in([数学],[物理],[英语],[语文]))b

天生格局:

/*
Student数学物理英语语文总成就
--------------------------------------------------------------
李四77856565292
张三87908278337

(2行受影响)
*/

go

--2、列转行
-->-->(Roy)天生y

ifnotobject_id(Class)isnull
droptableClass
Go
CreatetableClass([Student]nvarchar(2),[数学]int,[物理]int,[英语]int,[语文]int)
InsertClass
selectN李四,77,85,65,65unionall
selectN张三,87,90,82,78
Go

--2000:

静态:

declare@snvarchar(4000)
select@s=isnull(@s+unionall,)+select[Student],[Course]=+quotename(Name,)--isnull(@s+unionall,)往失落字符串@s中第一个unionall
+,[Score]=+quotename(Name)+fromClass
fromsyscolumnswhereID=object_id(Class)andNamenotin(Student)--扫除不转换的列
orderbyColid
exec(select*from(+@s+)torderby[Student],[Course])--增添一个排序

天生静态:
select*
from(select[Student],[Course]=数学,[Score]=[数学]fromClassunionall
select[Student],[Course]=物理,[Score]=[物理]fromClassunionall
select[Student],[Course]=英语,[Score]=[英语]fromClassunionall
select[Student],[Course]=语文,[Score]=[语文]fromClass)t
orderby[Student],[Course]

go
--2005:

静态:

declare@snvarchar(4000)
select@s=isnull(@s+,,)+quotename(Name)
fromsyscolumnswhereID=object_id(Class)andNamenotin(Student)
orderbyColid
exec(selectStudent,[Course],[Score]fromClassunpivot([Score]for[Course]in(+@s+))b)

go
select
Student,[Course],[Score]
from
Class
unpivot
([Score]for[Course]in([数学],[物理],[英语],[语文]))b

天生格局:
/*
StudentCourseScore
-------------------------
李四数学77
李四物理85
李四英语65
李四语文65
张三数学87
张三物理90
张三英语82
张三语文78

(8行受影响)
*/

使用DBaaS能让收入损失从其他业务上得到弥补,如软件更新和硬件管理。也许决定走DBaaS之路的客户可能会跳过解决方案提供商,尽管这个决策看起来有点短视。
莫相离 该用户已被删除
沙发
发表于 2015-1-19 06:26:15 | 只看该作者
在select语句中可以使用groupby子句将行划分成较小的组,然后,使用聚组函数返回每一个组的汇总信息,另外,可以使用having子句限制返回的结果集。
飘灵儿 该用户已被删除
板凳
发表于 2015-1-25 23:31:42 | 只看该作者
对一张百万级别的表建游标,同时又没有什么过滤条件,取得游标效率是如果直接SQL查询百万条数据;如果再对每条记录做处理,耗时将更长。
冷月葬花魂 该用户已被删除
地板
发表于 2015-2-4 13:22:09 | 只看该作者
始终遗憾SQLServer的登陆无法分配CPU/内存占用等指标数。如果你的SQLServer给别人分配了一个只可以读几个表的权限,而这个家伙疯狂的死循环进行连接查询,会给你的系统带来很大的负担。
深爱那片海 该用户已被删除
5#
发表于 2015-2-28 08:48:48 | 只看该作者
多走走一此相关论坛,多看一些实例开发,多交流0经验,没什么的,我也是刚学没多久!加油
简单生活 该用户已被删除
6#
发表于 2015-3-9 21:53:32 | 只看该作者
对递归类的树遍历很有帮助。个人感觉这个真是太棒了!阅读清晰,非常有时代感。
飘飘悠悠 该用户已被删除
7#
发表于 2015-3-17 02:08:58 | 只看该作者
原理很简单,对要求长时间计算某一时间点的报表生成和防用户操作错误很有帮助。但是比起Oracle10g的闪回技术还是细粒度不够。可惜!
小妖女 该用户已被删除
8#
发表于 2015-3-23 17:02:41 | 只看该作者
这就引发了对varchar和char效率讨论的老问题。到底如何分配varchar的数据,是否会出现大规模的碎片?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-20 19:50

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表