create table ta1( id int,操作类别 char(5),date char(10))
insert into ta1
select 101,'收入','2009-4-13'union
select 102,'发出','2009-4-14'union
select 103,'收入','2009-4-15'union
select 104,'发出','2009-4-15'
create table ta2( id int,名称 char(5), 数量 int)
INSERT INTO TA2
select 101,'ABC',1500 union
select 102,'ABC',800 union
select 103,'ABC',600 union
select 104,'ABC',500
select 名称, sum(case 操作类别 when '收入' then 数量 else 0 end ) as 收入数,
sum(case 操作类别 when '发出' then 数量 else 0 end) as 发出数
from ta2,(SELECT id, 操作类别 FROM TA1 group by 操作类别,id) as t
where ta2.id=t.id and 名称='ABC'
group by 名称