create table a (id1 char(4),type int,price int)
insert a select 'A001',0,25 
insert a select 'A002',1,70 
insert a select 'A003',1,100 
insert a select 'A004',0,800 create table b (id2 char(4),PRICE1        int,PRICE2  int)
insert b select 'A002',50,156 
insert b select 'A001',25,50 
insert b select 'A003',100,200 
insert b select 'A004',236,846select a.id1,max(type),max(price),sum(PRICE2-PRICE1) 
 from a join b on a.id1=b.id2
group by a.id1 
having max(price)<>sum(PRICE2-PRICE1)