问题:找出制造商A生产的PC和笔记本电脑的平均价格。 结果集:所有商品的整体平均价格。
产品表:
PC表:
打印机表:
笔记本电脑桌:
我的代码:
select (avg(pc.price)+avg(printer.price))/2 from pc, printer
where pc.model IN
(SELECT product.model from PRODUCT where maker='A')
AND
printer.model IN
(SELECT printer.model from product where maker = 'A')
答案 0 :(得分:1)
我认为要求不是2个平均值的平均值。
您所能做的就是按制造商“ A”选择所有价格的笔记本电脑和所有笔记本电脑,然后找到所有笔记本电脑的平均值。
您将必须使用UNION ALL
从两个表pc
和laptop
中选择所有价格:
select avg(t.price) AVG_price from (
select price from pc
where model in (select model from product where maker = 'A')
union all
select price from laptop
where model in (select model from product where maker = 'A')
) t
但是,如果是平均值的平均值,则遵循相同的逻辑,计算2个平均值并应用UNION ALL
以获得平均值:
select avg(t.price) AVG_price from (
select avg(price) price from pc
where model in (select model from product where maker = 'A')
union all
select avg(price) price from laptop
where model in (select model from product where maker = 'A')
) t