天天看點

SQLServer 列轉行使用--Pivot函數使用

select '小明' as name,'數學' as subjects,'100' as scores  
union 
select '小明' as name,'國文' as subjects,'95' as scores  
union 
select '小明' as name,'英語' as subjects,'90' as scores  
union
select '小張' as name,'數學' as subjects,'100' as scores  
union 
select '小張' as name,'國文' as subjects,'98' as scores  
union 
select '小張' as name,'英語' as subjects,'99' as scores  
           
SQLServer 列轉行使用--Pivot函數使用
select  * from 
(select '小明' as name,'數學' as subjects,100 as scores  
union 
select '小明' as name,'國文' as subjects,95 as scores  
union 
select '小明' as name,'英語' as subjects,90 as scores  
union
select '小張' as name,'數學' as subjects,100 as scores  
union 
select '小張' as name,'國文' as subjects,98 as scores  
union 
select '小張' as name,'英語' as subjects,99 as scores)  as tabs
pivot (
sum(scores)
 for subjects in ([數學],[國文],[英語])
)  as 分數
           
SQLServer 列轉行使用--Pivot函數使用