方法如下:
drop table m1
drop table m2
create table m1(itemno int,saleqty int,saleprice float(10) )
insert into m1(itemno,saleqty,saleprice)values(123,80,5.5)
insert into m1(itemno,saleqty,saleprice)values(321,70,5.6)
insert into m1(itemno,saleqty,saleprice)values(124,60,5.7)
select * from m1
create table m2(itemno int,price int,type int)
insert into m2(itemno,price,type)values(123,3,0)
insert into m2(itemno,price,type)values(123,4,1)
insert into m2(itemno,price,type)values(321,2,1)
insert into m2(itemno,price,type)values(124,4,2)
select * from m2
select m1.itemno,saleqty,saleprice, sum(case type when 0 then price else 0 end ) price1,
sum(case type when 1 then price else 0 end)price2,
sum(case type when 2 then price else 0 end )price3
from m1 inner join m2 on
m1.itemno=m2.itemno group by m1.itemno,m1.saleqty,saleprice order by m1.saleqty desc