SQL计数和平均值

时间:2014-01-28 17:12:27

标签: mysql sql count

我遇到了SQL查询的问题。

关系是

Journalist(JID(primary key), Name, Age, Salary, Chef -> Journalist.RID)
Article(AID(primary key), Title, Text)
Written(JID -> Journalist.JID, AID -> Article.AID, Date, Magazine)

现在我想实现两个问题:

  1. JID,每位撰写文章且年龄超过62岁的记者的姓名以及他撰写的文章数量。
  2. 每位厨师的JID,其中包括超过10名员工及其员工的平均年龄。
  3. 我刚刚开始学习SQL,所以我在解决问题时遇到了很多困难。现在我有了这个:

    SELECT j.JID, j.Name
    FROM Journalist j
    WHERE j.Age > 61
    

    以下是每位记者撰写的文章数量。我必须使用COUNT()吗?

    对于第二个查询,我有这样的事情:

    SELECT j.JID, count(*)
    FROM Journalist j
    GROUP BY Chef
    HAVING COUNT(*) > 9
    

    在这里,我需要他的员工的平均水平。我认为只使用AVG()会给出所有员工的平均值。

    感谢您的帮助!

6 个答案:

答案 0 :(得分:1)

对于第一个查询Journalist who has written an article and is older than 62,您必须稍微修改一下您的查询,因为您没有检查此人是否撰写了文章。

SELECT j.JID, j.Name
FROM Journalist j
WHERE j.Age > 61
    AND j.JID IN (SELECT w.JID FROM written w)

要获取必须使用的文章数COUNT,但首先必须形成关系以使用COUNT。试试这个

SELECT j.JID, J.Name, COUNT(a.AID) as [ArticlesWritten]
FROM Written w
    JOIN Article a ON a.AID = w.AID
    JOIN Journalist j ON w.JID = j.JID
GROUP BY j.JID, j.Name

对于有关chef with more than 10 employees under him的第二个查询,您需要向我们提供有关保存员工信息的更多信息。

答案 1 :(得分:0)

在第一个查询中,您还需要通过JID和Name加入Written表和组,它应该是:

SELECT j.JID, j.Name, COUNT(*) AS articles
  FROM Journalist j INNER JOIN Written w ON j.JID=w.JID
 WHERE j.Age > 61
 GROUP BY j.JID, j.Name

在第二个查询中,ypu需要选择您分组的列,并且还要添加AVG:

SELECT Chef, count(*) AS empoyees, avg(Age) AS AverageAge
  FROM Journalist j
 GROUP BY Chef
HAVING COUNT(*) > 9

我希望这可以帮到你

更新

为了避免第一个查询中的连接,你不能拥有记者的名字而只有JID,那么查询就是:

SELECT JID, COUNT(*) AS WrittenArticles
  FROM Written
 WHERE JID IN (SELECT JID FROM Journalist WHERE j.Age > 61)
 GROUP BY JID

如果没有加入,我无法弄清楚如何列出记者的姓名。

答案 2 :(得分:0)

对于第一个问题,你当然必须使用记者表(因为你需要名字)和书面表(你可以告诉谁写了什么)。所以你必须加入两个表(内连接),你必须要GROUP才能获得COUNT:

SELECT j.JID, j.NAME, count(*) as NumberOfArticleWritten
FROM   Journalist j
       inner join Written w on w.JID = j.JID
WHERE  j.Age >= 62
GROUP BY j.JID, j.NAME;

对于第二个问题,答案应如下所示:

select j_chef.JID, 
       count(*) as NumberOfEmployees, 
       avg(j_employee.Age) as EmployeesAverageAge
from   Journalist j_chef
group by j_chef.JID
having count(*) > 10

答案 3 :(得分:0)

第一次查询

SELECT j.JID, j.Name,COUNT(*) as NumberOfArticles
FROM Journalist j left join Written W on j.JID=W.JID
group by j.jid,j.Name
HAVING j.Age > 61 

第二个查询:假设记者表的JID存储在主列中,即使用自引用表

SELECT Chief.JID, AVG(J.Age) 
FROM Journalist Chief left join Journalist j on J.RID=chief.JID 
GROUP BY Chief.JID HAVING COUNT(*)>10

答案 4 :(得分:0)

首先查询答案

SELECT j.JID, j.NAME, count(*) as NumberOfArticle
FROM   Journalist j
       inner join Written w on w.JID = j.JID
WHERE  j.Age >= 62
GROUP BY j.JID, j.NAME;

答案 5 :(得分:0)

非常感谢! 我应该避免JOIN操作,所以经过一些工作后,我得出了以下解决方案:

  1. (每位记者的年龄超过62岁的JID和他撰写的文章数量)
  2. SELECT j.JID, j.Name, count(*) AS WrittenArticles
    FROM Journalist j
    WHERE j.Age > 62
          AND j.JID IN (SELECT w.JID FROM Written w)
    GROUP BY j.JID, j.Name
    
    1. 拥有10名以上员工的每位厨师的JID以及他所拥有的员工的平均年龄。 我想到了类似的东西:
    2. SELECT c.JID, AVG(j.Age)
      FROM Journalist c, Journalist j
      WHERE 10 < (SELECT count(*) FROM Journalist GROUP BY Chef)
            AND j.Chef = c.RID
      
      我可以这样做吗?