class表数据

SqlServer2005 行转列,列转行

class1表数据

SqlServer2005 行转列,列转行


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]
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]

-----以上sql2000 行转列

-----下面sql2005 列转行
select
*
from
Class1
unpivot
([Score] for [Course] in([数学],[物理],[英语],[语文]))b


-----下面sql2005 行转列
select *
from
Class
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b

相关文章:

  • 2021-07-25
  • 2022-12-23
  • 2021-12-22
  • 2021-04-10
  • 2021-07-04
  • 2021-08-07
  • 2021-04-15
猜你喜欢
  • 2021-08-11
  • 2022-12-23
  • 2021-11-12
  • 2021-07-21
  • 2022-12-23
相关资源
相似解决方案