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);table
select * from (select name,QU, nums from demo) pivot (sum(nums) for name in ('蘋果' 蘋果, '橘子', '葡萄', '芒果')) ORDER BY QU ;select