我有以下问题:
1)我已成功设法创建一个结果,显示多部电影的收入(计算出租的频率*出租率)。 [例A]
2)现在我想得到平均值。 如果我围绕它编写另一个查询,我已经设法做到了[例B],但是如果可能的话我想将它包含在同一个查询中。 [例C - 给我一个错误]。这有可能吗?
示例A:
SELECT COUNT(r.rental_id) * f.rental_rate AS Revenue
FROM film f, rental r, inventory i
WHERE i.inventory_id = r.inventory_id
AND f.film_id = i.film_id
GROUP BY f.title
ORDER BY Revenue DESC
示例B :(我如何使其工作 - 封装查询)
SELECT AVG(B.Revenue)
FROM (
SELECT COUNT(r.rental_id) * f.rental_rate AS Revenue
FROM film f, rental r, inventory i
WHERE i.inventory_id = r.inventory_id
AND f.film_id = i.film_id
GROUP BY f.title
ORDER BY Revenue DESC ) B
示例C :(我希望它如何工作 - 一个查询)
SELECT AVG (COUNT(r.rental_id) * f.rental_rate) AS Revenue
FROM film f, rental r, inventory i
WHERE i.inventory_id = r.inventory_id
AND f.film_id = i.film_id
GROUP BY f.title
ORDER BY Revenue DESC
示例C给出:"错误代码:1111 \ n无效使用组功能"
答案 0 :(得分:0)
仔细查看错误消息无效使用组功能。问题出现在查询的第一行,如下所示。您无法按照尝试的方式嵌套组功能。你必须在外部查询中得到它。
AVG (COUNT(r.rental_id) * f.rental_rate)
像
这样的东西SELECT AVG(Revenue) FROM (
SELECT COUNT(r.rental_id) * f.rental_rate AS Revenue
FROM film f
JOIN inventory i ON f.film_id = i.film_id
JOIN rental r ON i.inventory_id = r.inventory_id
GROUP BY f.title ) xxx
ORDER BY Revenue DESC;