sql查询显示平均数量

时间:2016-03-15 10:30:15

标签: sql join average

所以我有这两个表,其中包含以下列:

  1. 重复次数:
  2. 代表身份证明 代表名称

    1. 交易:
    2. 交易ID 价钱 REPID

      我希望所有拥有交易的代表共有一个平均数(200)。

      是吗:

       SELECT * from Reps
       INNER JOIN Transactions
       ON Reps.Rep ID = Transactions.RepId
       WHERE Transactions.Price>(SELECT AVG(200) FROM Transactions);
      

      谢谢!

2 个答案:

答案 0 :(得分:2)

您似乎希望让Reps的平均值Price超过200

SELECT RepID 
FROM Reps
INNER JOIN Transactions
ON Reps.RepID = Transactions.RepId
GROUP BY RepID
HAVING AVG(Price) > 200

答案 1 :(得分:0)

  select r.repid 
    from reps r,
(select avg(price),repid from transactions group by repid having avg(price)>200) t 
    where r.repid=t.repid