行列互转整理

 
  1. /******************************************************************************************************************************************************
  2. 以学生成绩为例子,比较形象易懂
  3. 整理人:中国风(Roy)
  4. 日期:2008.06.06
  5. ******************************************************************************************************************************************************/
  6. --1、行互列
  7. --> --> (Roy)生成測試數據
  8. if not object_id('Class'is null
  9.     drop table Class
  10. Go
  11. Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
  12. Insert Class
  13. select N'张三',N'语文',78 union all
  14. select N'张三',N'数学',87 union all
  15. select N'张三',N'英语',82 union all
  16. select N'张三',N'物理',90 union all
  17. select N'李四',N'语文',65 union all
  18. select N'李四',N'数学',77 union all
  19. select N'李四',N'英语',65 union all
  20. select N'李四',N'物理',85 
  21. Go
  22. --2000方法:
  23. 动态:
  24. declare @s nvarchar(4000)
  25. set @s=''
  26. Select     @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
  27. from Class group by[Course]
  28. exec('select [Student]'+@s+' from Class group by [Student]')
  29. 生成静态:
  30. select 
  31.     [Student],
  32.     [数学]=max(case when [Course]='数学' then [Score] else 0 end),
  33.     [物理]=max(case when [Course]='物理' then [Score] else 0 end),
  34.     [英语]=max(case when [Course]='英语' then [Score] else 0 end),
  35.     [语文]=max(case when [Course]='语文' then [Score] else 0 end) 
  36. from 
  37.     Class 
  38. group by [Student]
  39. GO
  40. 动态:
  41. declare @s nvarchar(4000)
  42. Select     @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
  43. exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')
  44. 生成静态:
  45. select * 
  46. from 
  47.     Class 
  48. pivot 
  49.     (max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b
  50. 生成格式:
  51. /*
  52. Student 数学          物理          英语          语文
  53. ------- ----------- ----------- ----------- -----------
  54. 李四      77          85          65          65
  55. 张三      87          90          82          78
  56. (2 行受影响)
  57. */
  58. ------------------------------------------------------------------------------------------
  59. go
  60. --加上总成绩(学科平均分)
  61. --2000方法:
  62. 动态:
  63. declare @s nvarchar(4000)
  64. set @s=''
  65. Select     @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
  66. from Class group by[Course]
  67. exec('select [Student]'+@s+',[总成绩]=sum([Score])  from Class group by [Student]')--加多一列(学科平均分用avg([Score]))
  68. 生成动态:
  69. select 
  70.     [Student],
  71.     [数学]=max(case when [Course]='数学' then [Score] else 0 end),
  72.     [物理]=max(case when [Course]='物理' then [Score] else 0 end),
  73.     [英语]=max(case when [Course]='英语' then [Score] else 0 end),
  74.     [语文]=max(case when [Course]='语文' then [Score] else 0 end),
  75.     [总成绩]=sum([Score]) --加多一列(学科平均分用avg([Score]))
  76. from 
  77.     Class 
  78. group by [Student]
  79. go
  80. --2005方法:
  81. 动态:
  82. declare @s nvarchar(4000)
  83. Select     @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course] --isnull(@s+',','') 去掉字符串@s中第一个逗号
  84. exec('select [Student],'+@s+',[总成绩] from (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a 
  85. pivot (max([Score]) for [Course] in('+@s+'))b ')
  86. 生成静态:
  87. select 
  88.     [Student],[数学],[物理],[英语],[语文],[总成绩] 
  89. from 
  90.     (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a --平均分时用avg([Score])
  91. pivot 
  92.     (max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b 
  93. 生成格式:
  94. /*
  95. Student 数学          物理          英语          语文          总成绩
  96. ------- ----------- ----------- ----------- ----------- -----------
  97. 李四      77          85          65          65          292
  98. 张三      87          90          82          78          337
  99. (2 行受影响)
  100. */
  101. go
  102. --2、列转行
  103. --> --> (Roy)生成測試數據
  104. if not object_id('Class'is null
  105.     drop table Class
  106. Go
  107. Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
  108. Insert Class
  109. select N'李四',77,85,65,65 union all
  110. select N'张三',87,90,82,78
  111. Go
  112. --2000:
  113. 动态:
  114. declare @s nvarchar(4000)
  115. select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
  116. +',[Score]='+quotename(Name)+' from Class'
  117. from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
  118. order by Colid
  119. exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序
  120. 生成静态:
  121. select * 
  122. from (select [Student],[Course]='数学',[Score]=[数学] from Class union all 
  123. select [Student],[Course]='物理',[Score]=[物理] from Class union all 
  124. select [Student],[Course]='英语',[Score]=[英语] from Class union all 
  125. select [Student],[Course]='语文',[Score]=[语文] from Class)t 
  126. order by [Student],[Course]
  127. go
  128. --2005:
  129. 动态:
  130. declare @s nvarchar(4000)
  131. select @s=isnull(@s+',','')+quotename(Name)
  132. from syscolumns where ID=object_id('Class') and Name not in('Student'
  133. order by Colid
  134. exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')
  135. go
  136. select 
  137.     Student,[Course],[Score] 
  138. from 
  139.     Class 
  140. unpivot 
  141.     ([Score] for [Course] in([数学],[物理],[英语],[语文]))b
  142. 生成格式:
  143. /*
  144. Student Course Score
  145. ------- ------- -----------
  146. 李四      数学      77
  147. 李四      物理      85
  148. 李四      英语      65
  149. 李四      语文      65
  150. 张三      数学      87
  151. 张三      物理      90
  152. 张三      英语      82
  153. 张三      语文      78
  154. (8 行受影响)
  155. */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值