我知道固定列标题的转法,列标题不固定的不会(SQL SERVER中会) 如表字段test(f1 varchar,f2 bunmer) 内容为:f1 f2 A 10 B 15 C 11 转换成列为:A B C 10 15 11程序为: select sum(case when f1 = 'A' then f2 else 0 end) A, sum(case when f1 = 'B' then f2 else 0 end) B, sum(case when f1 = 'C' then f2 else 0 end) C from test group by f1;
如表字段test(f1 varchar,f2 bunmer)
内容为:f1 f2
A 10
B 15
C 11
转换成列为:A B C
10 15 11程序为:
select sum(case when f1 = 'A' then f2 else 0 end) A,
sum(case when f1 = 'B' then f2 else 0 end) B,
sum(case when f1 = 'C' then f2 else 0 end) C
from test
group by f1;
张三 语文 20
张三 数学 30
张三 英语 50
李四 语文 70
李四 数学 60
李四 英语 90
转换成:姓名 语文 数学 英语
张三 20 30 50
李四 70 60 90
SQL> select name,
2 sum(decode(subject,'语文',grade,0))yuwen,
3 sum(decode(subject,'数学',grade,0))shuxue,
4 sum(decode(subject,'英语',grade,0))yingyu
5 from test group by name;