欢迎来到福编程网,本站提供各种互联网专业知识!
您的位置:网站首页 > 数据库 > Oracle

Oracle的数据表中行转列与列转行的操作实例讲解

发布时间:2015-12-17 作者:真正男子汉 来源:转载
这篇文章主要介绍了Oracle数据表中行转列与列转行的操作方法,这里分静态和动态情况作出了分类讨论,需要的朋友可以参考下

行转列

一张表

20151217170849821.jpg (220×151)

查询结果为

20151217170911011.jpg (170×63)

--行转列

代码
  1. select years,(select amount from Tb_Amount as A where month=1 and A.years=Tb_Amount.years)as m1,
  2. (select amount from Tb_Amount as A where month=2 and A.years=Tb_Amount.years)as m2,
  3. (select amount from Tb_Amount as A where month=3 and A.years=Tb_Amount.years)as m3
  4. from Tb_Amount group by years

或者为

代码
  1. select years as 年份,
  2. sum(case when month='1' then amount end) as 一月,
  3. sum(case when month='2' then amount end) as 二月,
  4. sum(case when month='3' then amount end) as 三月
  5. from dbo.Tb_Amount group by years order by years desc

2.人员信息表包括姓名 时代 金额

20151217170947066.jpg (254×150)

显示行转列

姓名 时代 金额

姓名 年轻 中年 老年

张丽 1000000.00 4000000.00 500000000.00

孙子 2000000.00 12233335.00 4552220010.00

20151217171005767.jpg (322×84)

代码
  1. select uname as 姓名,
  2. SUM(case when era='年轻' then amount end) as 年轻,
  3. SUM(case when era='中年' then amount end) as 中年,
  4. SUM(case when era='老年' then amount end) as 老年
  5. from Tb_People group by uname order by uname desc

3.学生表 [Tb_Student]

20151217171053471.jpg (204×144)

显示效果

20151217171109012.jpg (191×56)

静态SQL,指subject只有语文、数学、英语这三门课程。

代码
  1. select sname as 姓名,
  2. max(case Subject when '语文' then grade else 0 end) as 语文,
  3. max(case Subject when '数学' then grade else 0 end) as 数学,
  4. max(case Subject when '英语' then grade else 0 end) as 英语
  5. from dbo.Tb_Student group by sname order by sname desc

--动态SQL,指subject不止语文、数学、英语这三门课程。

代码
  1. declare @sql varchar(8000)
  2. set @sql = 'select sname as ' + '姓名'
  3. select @sql = @sql + ' , max(case Subject when ''' + Subject + ''' then grade else 0 end) [' + Subject + ']'
  4. from (select distinct Subject from Tb_Student) as a
  5. set @sql = @sql + ' from Tb_Student group by sname order by sname desc'
  6. exec(@sql)

oracle中Decode()函数使用 然后将这些累计求和(sum部分)

代码
  1. select t.sname AS 姓名,
  2. sum(decode(t.subject,'语文',grade,null))语文 ,
  3. sum(decode(t.subject,'数学',grade,null)) 数学,
  4. sum(decode(t.subject,'英语',grade,null)) 英语
  5. from Tb_Student t group by sname order by sname desc

列转行

20151217171127272.jpg (225×66)

生成

20151217171144405.jpg (223×134)

sql代码

生成静态:

代码
  1. select *
  2. from (select sname,[Course ] ='数学',[Score]=[数学] from Tb_students union all
  3. select sname,[Course]='英语',[Score]=[英语] from Tb_students union all
  4. select sname,[Course]='语文',[Score]=[语文] from Tb_students)t
  5. order by sname,case [Course] when '语文' then 1 when '数学' then 2 when '英语' then 3 end
  6. go
  7. --列转行的静态方案:UNPIVOT,sql2005及以后版本
  8. SELECT sname,Subject, grade
  9. from dbo.Tb_students
  10. unpivot(grade for Subject in([语文],[数学],[英语]))as up
  11. GO
  12. --列转行的动态方案:UNPIVOT,sql2005及以后版本
  13. --因为行是动态所以这里就从INFORMATION_SCHEMA.COLUMNS视图中获取列来构造行,同样也使用了XML处理。
  14. declare @s nvarchar(4000)
  15. select @s=isnull(@s+',','')+quotename(Name)
  16. from syscolumns where ID=object_id('Tb_students') and Name not in('sname')
  17. order by Colid
  18. exec('select sname,[Subject],[grade] from Tb_students unpivot ([grade] for [Subject] in('+@s+'))b')
  19. go
  20. select
  21. sname,[Subject],[grade]
  22. from
  23. Tb_students
  24. unpivot
  25. ([grade] for [Subject] in([数学],[英语],[语文]))b

相关推荐