create table demo(id int,QU varchar(20),name varchar(20),nums int); ---- 创建表
insert into demo values(1, 'Q1','苹果', 1000);
insert into demo values(2, 'Q2', '苹果', 2000);
insert into demo values(3, 'Q3','苹果', 4000);
insert into demo values(10, 'Q1', '苹果2', 4000);
insert into demo values(4, 'Q3','橘子', 5000);
insert into demo values(5, 'Q1', '橘子', 3000);
insert into demo values(6, 'Q1','葡萄', 3500);
insert into demo values(7, 'Q4', '芒果', 4200);
insert into demo values(8, 'Q1','芒果', 5500);
select * from (select name,QU, nums from demo) pivot (sum(nums) for name in ('苹果' 苹果, '橘子', '葡萄', '芒果')) ORDER BY QU ;