sqlserver 行转列函数
时间: 2024-04-08 17:25:37 浏览: 110
以下是两种SQL Server行转列的实现方式:
1. 使用CASE WHEN动态行转列:
```sql
declare @sql varchar(4000)
set @sql = 'select NAME'
select @sql = @sql + ',sum(case COURSE when '''+COURSE+''' then SCORE end) ['+COURSE+']'
from (select distinct COURSE from STUDENT ) as a
order by COURSE
select @sql = @sql+' from STUDENT group by NAME'
exec(@sql)
```
2. 使用内置函数PIVOT:
```sql
SELECT * FROM student PIVOT (SUM(score) FOR course IN (A,B,C,D,E,F,G,H,I) ) as t
```
阅读全文