这是一个显示每个强盗,他们的监禁年数,所犯抢劫的数量和他们的总收入(21行)的视图。
然后我尝试创建另一个视图以进一步操纵上面视图中的数据。我希望我的新视图只显示劫匪的绰号,其NoYears(在监狱服刑)= 0且其NoRobberies大于平均抢劫数。
CREATE VIEW ActiveRobbers3
AS
SELECT Nickname
FROM ActiveRobbers2
WHERE NoYears = 0
GROUP BY Nickname, NoRobberies
HAVING NoRobberies > AVG(NoRobberies);
但是当我尝试SELECT * FROM ActiveRobbers3
时,这不会返回任何行。谁能看到我的问题?干杯
答案 0 :(得分:3)
你的问题很模糊。表中所有数据的平均NoRobberies
或与特定“昵称”相关的数据。
案例1:所有数据中NoRobberies
的平均值
CREATE VIEW ActiveRobbers3
AS
SELECT Nickname
FROM ActiveRobbers2
WHERE NoYears = 0
AND NoRobberies > (select AVG(NoRobberies) from ActiveRobbers2);
案例2:各个昵称“昵称”的平均值NoRobberies
CREATE VIEW ActiveRobbers3
AS
SELECT Nickname,NoRobberies
FROM ActiveRobbers2
WHERE NoYears = 0
GROUP BY Nickname
Having NoRobberies > AVG(NoRobberies);
注意:只有当昵称至少有两行具有不同的NoRobberies时,case-2才会提供数据。示例代码适用于MySQL
答案 1 :(得分:0)
也许这有效:
CREATE VIEW ActiveRobbers3
AS
SELECT Nickname
FROM ActiveRobbers2,
(SELECT AVG(AVG_NoRobberies) as AVG_NoRobberies FROM ActiveRobbers2) as b
WHERE NoYears = 0
AND NoRobberies > b.AVG_NoRobberies