2

我对网站http://www.sql-ex.ru/上的 sql 查询有疑问。查询要求:

定义制造商 A 生产的个人电脑和笔记本电脑的平均价格。

数据库架构如下:

Product(maker, model, type)
PC(code, model, speed, ram, hd, cd, price)
Laptop(code, model, speed, ram, hd, screen, price)
Printer(code, model, color, type, price)

我将查询写为:

SELECT AVG(t.k) AS Avg_Price
FROM
  (SELECT AVG(A.price) AS k
    FROM PC A 
  JOIN Product B ON(A.model=B.model)
  WHERE B.maker='A'
  UNION ALL
  SELECT AVG(C.price) AS k
     FROM Laptop C
  JOIN PRODUCT D ON(C.model=D.model)
  WHERE D.maker='A') AS t

问题是它没有返回正确的答案。平均回报远高于预期。计算平均值的方式是否错误?如何更改查询以使其返回预期的答案?任何帮助,将不胜感激。

谢谢

4

11 回答 11

4

您分别计算个人电脑价格和笔记本电脑价格的平均值,然后将平均值放在一起。您的查询很好,只是您不应该对子查询中的价格进行平均。只需返回子查询中的价格和顶层的平均值:

select
    AVG( Price ) Avg_Price
from
(
    (
        select
            pc.Price
        from
            PC pc
            join Produt prod
             on pc.Model = prod.Model
        where
            prod.Maker = 'A'
    )
    union all
    (
        select
            pc.Price
        from
            Laptop l
            join Produt prod
             on l.Model = prod.Model
        where
            prod.Maker = 'A'
    )
) q
于 2013-03-27T06:10:03.953 回答
3
SELECT AVG(datatable.price) FROM
(
(SELECT PC.Price FROM PC INNER JOIN Product p1 ON PC.model=P1.model     
     WHERE P1.maker='A') 
UNION ALL 
(SELECT Laptop.price FROM Laptop INNER JOIN Product p2 ON 
    Laptop.model=P2.model WHERE P2.maker='A')
) datatable

正确的。

您的查询结果:

平均价格

754.1666

于 2014-02-20T08:13:43.183 回答
2

你的查询是错误的。它通常就像您如何在分布式系统上进行聚合一样。您可以在分布式节点上进行聚合,然后带回聚合数据并在返回数据的顶部进行 agg,除非聚合是可转移的。AVG不可传输。(1+2+3+4+5)/5 <> ((1+2)/2 + (1+2+3)/3)

于 2013-03-27T06:09:14.620 回答
1

它也有效

SELECT Avg(price) AS avg_price
FROM   (SELECT pc.price
        FROM   pc
               INNER JOIN product
                       ON pc.model = product.model
        WHERE  maker = 'A'
        UNION ALL
        SELECT laptop.price
        FROM   laptop
               INNER JOIN product
                       ON laptop.model = product.model
        WHERE  maker = 'A') Any_name 
于 2019-05-03T06:28:20.427 回答
1
with t1 as
(SELECT price, type from pc left join product on product.model = pc.model where maker = 'A'
UNION ALL
SELECT price, type from laptop left join product on product.model = laptop.model where maker = 'A')
select avg(price) from t1
于 2016-10-14T10:09:01.053 回答
0
SELECT AVG(resultList.Price)
FROM
    (
        SELECT  a.Price
        FROM    PC a INNER JOIN Product B 
                    on a.Model = B.Model
        WHERE   B.Maker = 'A'
        UNION ALL
        SELECT  c.Price
        FROM    Laptop c INNER JOIN Product d 
                    on c.Model = d.Model
        WHERE   d.Maker = 'A'
    ) resultList
于 2013-03-27T06:13:21.547 回答
0

我总是与 JOINS(个人)有很大的斗争,所以试试这个没有加入的解决方案。使用 IN 运算符

with cte as (
    SELECT price, model, code 
    FROM pc 
    WHERE model 
    IN (SELECT model FROM product WHERE maker = 'A')
UNION
    SELECT price, model, code 
    FROM laptop 
    WHERE model 
    IN (SELECT model FROM product WHERE maker = 'A')
)

SELECT AVG(price) FROM cte
于 2020-07-17T05:20:12.637 回答
0
select AVG(P.price) as avg_price
from
(
 select price, model from PC
 UNION ALL
 select price, model from Laptop
) P
join Product ON P.model = Product.model
where maker = 'A'
于 2015-10-08T09:34:08.863 回答
0
Select avg(price) from
       ((select price,model  from pc 
       union all
       select price,model from laptop) a inner join  product on product.model=a.model and product.maker='A' )

首先,我认为如果我union pricemodel两张桌子,然后我们可以average(price)从制造商 A那里找到会更好。

于 2019-02-24T11:58:49.307 回答
0

SELECT AVG(price) FROM 
(
     SELECT PC.price, Product.maker FROM PC JOIN Product ON(PC.model=Product.model)
     UNION ALL
     SELECT Laptop.price, Product.maker FROM Laptop JOIN Product ON (Laptop.model = Product.model)
) T 
GROUP BY T.maker HAVING T.maker = 'A'

它之所以有效,是因为首先您选择了所有笔记本电脑和个人电脑的价格,接下来您只选择制造商 = 'A' 的记录,最后一步是计算平均价格。

于 2018-05-07T17:55:02.827 回答
-1
select avg(price) as avg_price from
(select price
from Product, PC
where Product.model = PC.model
and maker = 'A'
union all
select price
from Product, Laptop
where Product.model = Laptop.model
and maker='A') avg_price 
于 2019-05-03T06:32:19.473 回答