古老的行转列

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