提醒:题目中提供的table3有部分结果是错误的,请注意。
CREATE TABLE table1(mon varchar(100),dep varchar(100),yj int)
insert into table1
select '一月份','01',10
union all
select '一月份','02',10
union all
select '一月份','03',5
union all
select '二月份','02',8
union all
select '二月份','04',9
union all
select '三月份','03',8
CREATE TABLE table2(dep varchar(100),dname varchar(100))
insert into table2
select '01','国内业务一部'
union all
select '02','国内业务二部'
union all
select '03','国内业务三部'
union all
select '04','国内业务部'
select * from table1
select * from table2
select a.dep,
'一月份'=(select yj from table1 b where b.dep=a.dep and b.mon='一月份'),
'二月份'=(select yj from table1 b where b.dep=a.dep and b.mon='二月份'),
'三月份'=(select yj from table1 b where b.dep=a.dep and b.mon='三月份')
from table2 a
drop table table1,table2