create table NewsStands(
standNo number(1) primary key,
stand_name varchar2(10) not null);
insert into NewsStands values(1,'売店1');
insert into NewsStands values(2,'売店2');
insert into NewsStands values(3,'売店3');
insert into NewsStands values(4,'売店4');
insert into NewsStands values(5,'売店5');
insert into NewsStands values(6,'売店6');
insert into NewsStands values(7,'売店7');
create table Sales(
standNo number(1) not null,
products_ID number(5) not null,
net_sold_qty number(1) not null);
insert into Sales values(1, 2667,0);
insert into Sales values(1, 2667,0);
insert into Sales values(1, 2667,1);
insert into Sales values(1,48632,5);
insert into Sales values(1, 1107,5);
insert into Sales values(2, 2667,1);
insert into Sales values(2,48632,1);
insert into Sales values(2, 1107,6);
insert into Sales values(2,12345,4);
insert into Sales values(3, 2667,3);
insert into Sales values(3,48632,1);
insert into Sales values(3, 1107,4);
insert into Sales values(4, 2667,3);
insert into Sales values(4,48632,4);
insert into Sales values(4, 1107,4);
insert into Sales values(5, 1107,9);
insert into Sales values(6, 2667,3);
insert into Sales values(6,48632,1);
insert into Sales values(6,48632,4);
insert into Sales values(6,12345,4);
commit;
select a.stand_name
from NewsStands a,Sales b
where a.standNo = b.standNo
and b.products_ID in(2667,48632,1107)
group by a.standNo,a.stand_name
having
2 < all(avg(decode(b.products_ID, 2667,b.net_sold_qty)),
avg(decode(b.products_ID,48632,b.net_sold_qty)))
or 5 < avg(decode(b.products_ID,1107,b.net_sold_qty))
order by a.standNo;