`
simlee
  • 浏览: 53930 次
  • 性别: Icon_minigender_1
  • 来自: 地球
社区版块
存档分类
最新评论

行列互转--整理

阅读更多
--行列互转
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂

整理人:中国风(Roy)

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

--1、行互列
--> --> (Roy)生成測試數據
 
if not object_id('Class') is null
    drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert Class
select N'张三',N'语文',78 union all
select N'张三',N'数学',87 union all
select N'张三',N'英语',82 union all
select N'张三',N'物理',90 union all
select N'李四',N'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85 
Go
--2000方法:
动态:

declare @s nvarchar(4000)
set @s=''
Select     @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')


生成静态:

select 
    [Student],
    [数学]=max(case when [Course]='数学' then [Score] else 0 end),
    [物理]=max(case when [Course]='物理' then [Score] else 0 end),
    [英语]=max(case when [Course]='英语' then [Score] else 0 end),
    [语文]=max(case when [Course]='语文' then [Score] else 0 end) 
from 
    Class 
group by [Student]

GO
动态:

declare @s nvarchar(4000)
Select     @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')

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

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

(2 行受影响)
*/

------------------------------------------------------------------------------------------
go
--加上总成绩(学科平均分)

--2000方法:
动态:

declare @s nvarchar(4000)
set @s=''
Select     @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+',[总成绩]=sum([Score])  from Class group by [Student]')--加多一列(学科平均分用avg([Score]))

生成动态:

select 
    [Student],
    [数学]=max(case when [Course]='数学' then [Score] else 0 end),
    [物理]=max(case when [Course]='物理' then [Score] else 0 end),
    [英语]=max(case when [Course]='英语' then [Score] else 0 end),
    [语文]=max(case when [Course]='语文' then [Score] else 0 end),
    [总成绩]=sum([Score]) --加多一列(学科平均分用avg([Score]))
from 
    Class 
group by [Student]

go

--2005方法:

动态:

declare @s nvarchar(4000)
Select     @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course] --isnull(@s+',','') 去掉字符串@s中第一个逗号
exec('select [Student],'+@s+',[总成绩] from (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a 
pivot (max([Score]) for [Course] in('+@s+'))b ')

生成静态:

select 
    [Student],[数学],[物理],[英语],[语文],[总成绩] 
from 
    (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a --平均分时用avg([Score])
pivot 
    (max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b 

生成格式:

/*
Student 数学          物理          英语          语文          总成绩
------- ----------- ----------- ----------- ----------- -----------
李四      77          85          65          65          292
张三      87          90          82          78          337

(2 行受影响)
*/

go

--2、列转行
--> --> (Roy)生成測試數據
 
if not object_id('Class') is null
    drop table Class
Go
Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
Insert Class
select N'李四',77,85,65,65 union all
select N'张三',87,90,82,78
Go

--2000:

动态:

declare @s nvarchar(4000)
select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
+',[Score]='+quotename(Name)+' from Class'
from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
order by Colid
exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序

生成静态:
select * 
from (select [Student],[Course]='数学',[Score]=[数学] from Class union all 
select [Student],[Course]='物理',[Score]=[物理] from Class union all 
select [Student],[Course]='英语',[Score]=[英语] from Class union all 
select [Student],[Course]='语文',[Score]=[语文] from Class)t 
order by [Student],[Course]

go
--2005:

动态:

declare @s nvarchar(4000)
select @s=isnull(@s+',','')+quotename(Name)
from syscolumns where ID=object_id('Class') and Name not in('Student') 
order by Colid
exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')

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

生成格式:
/*
Student Course Score
------- ------- -----------
李四      数学      77
李四      物理      85
李四      英语      65
李四      语文      65
张三      数学      87
张三      物理      90
张三      英语      82
张三      语文      78

(8 行受影响)
*/
分享到:
评论

相关推荐

    电子琴、编曲、序列生成、格式转换、格式整理软件(编程辅助工具)

    6、 多种格式之间的转换,如简繁的互转,GB转换成16进制的互转,GB与UCS2的互转,中英文符号的互转,大小写互转,16进制C与汇编格式互转,16进制与10进制格式互转等。 7、 使用本软件编辑时,可直接将查找的内容...

    考研备考资料真题-考研数学-湖南大学高等代数2005--2009年考研真题[1].docx

    - 真题汇编是指将历年真题集中整理,便于考生复习。 #### 二、知识点详细说明 ##### 第一部分:2005年真题 - **第一题**:考查多项式理论中的导数概念和整除性质。 - 题目要求证明一个多项式能被其导数整除的...

    本人花了4小时左右整理的,EE专业课名词全收集(部分)

    * 互信息 * 信源的信息率 * 信息传输率 * 信道容量 * 数据处理定理 * 信息不增定理 * 波形信源和波形信道 * 连续信源熵 * 高斯噪声信源的熵 * 限峰值功率的最大熵定理 * 限平均功率的最大熵定理 线性代数 * 行列式...

    Deep Learning - Ian Goodfellow (2017-10-31)

    3. 矩阵的迹(Trace)、行列式(Determinant)以及Moore-Penrose伪逆矩阵:这些是线性代数中比较高级的内容,理解这些概念对于深入理解线性变换、方程解的稳定性以及如何在矩阵不满足条件时求解线性方程组非常有帮助...

    高三数学第二次模拟考试试卷(扫描版)文 试题.doc

    10. **矩阵与行列式**:矩阵的基本运算,行列式的定义与性质,解线性方程组。 这些知识点的掌握对于高三学生来说至关重要,因为它们不仅出现在高考中,也是大学数学学习的基础。通过模拟考试,学生可以发现自己的...

    采用MSP430F413单片机控制的温度采集系统设计

    以下是根据文档内容整理出的相关知识点: 1. MSP430F413单片机特性: MSP430F413是德州仪器(TI)生产的一款低功耗16位微控制器,具有丰富的外设和较高的处理能力。该系列单片机广泛应用于各种测控系统中,尤其适合于...

    数学课件学校.rar

    4. **概率与统计**:这部分内容涉及到数据收集、整理、描述和分析,以及概率论的基本概念,如概率、随机事件、独立事件、条件概率等。这些知识在日常生活中有广泛应用,如预测、决策等。 5. **数列与极限**:数列是...

Global site tag (gtag.js) - Google Analytics