MAX() 最新日期,无效对象
我正在尝试创建一个表,其中包含每个客户的最新电影租赁日期。
目前,我有一个视图,涵盖了大部分所需数据,除了每个客户每部电影的返回日期之外,此信息位于一个名为“租金”的表中,我无法在“FROM”部分中引用该表,因为它与“视图”冲突。
这就是我目前所做的,但我不断在某些表上收到无效对象错误。例如租金和租金视图。
SELECT customer_name
, COUNT(DISTINCT rental_id) AS Number_of_Rentals
, latest_rental
FROM Rental_view,
(SELECT MAX(return_date) AS latest_rental FROM rentals)
latest_rental
GROUP BY customer_id, customer_name
ORDER BY customer_id, customer_name
不确定这里出了什么问题,在正确的位置有第二个嵌入式 SELECT 语句,我是否正确使用它?
这是上下文问题。
“生成一个查询,显示所有客户的客户名称、租赁次数、最近租赁日期和平均租赁持续时间。”
更新了代码。
SELECT customer_name
, COUNT(DISTINCT r.rental_id) AS Number_of_Rentals
, MAX(r.return_date) AS latest_rental
, AVG(rv.rental_duration) AS AVG_Rental_duration
FROM Rental_View AS rv JOIN rentals AS r
ON r.rental_id = rv.rental_ID
GROUP BY rv.customer_id, customer_name
ORDER BY rv.customer_id, customer_name
i am trying to create a table that has the latest date of a movie rental for each customer.
currently i have a view that covers most of the required data except for the return date of each movie per customer, this information is located in a table called rentals which i am unable to reference in the FROM section because it conflicts with the VIEW.
this is currently what i have done but i keep getting an invalid object error on some tables. such as rentals and rental_view.
SELECT customer_name
, COUNT(DISTINCT rental_id) AS Number_of_Rentals
, latest_rental
FROM Rental_view,
(SELECT MAX(return_date) AS latest_rental FROM rentals)
latest_rental
GROUP BY customer_id, customer_name
ORDER BY customer_id, customer_name
Not sure what is wrong here, have a got the Second embedded SELECT statement in the correct place and am i using it properly?
here is the question for context.
"Produce a query that shows the customer name, number of rentals, latest rental date and average rental duration of all customers."
Updated Code.
SELECT customer_name
, COUNT(DISTINCT r.rental_id) AS Number_of_Rentals
, MAX(r.return_date) AS latest_rental
, AVG(rv.rental_duration) AS AVG_Rental_duration
FROM Rental_View AS rv JOIN rentals AS r
ON r.rental_id = rv.rental_ID
GROUP BY rv.customer_id, customer_name
ORDER BY rv.customer_id, customer_name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 customer_id 在租赁和 Rental_View 之间进行
JOIN
。如果您只想包含两个表中都具有 customer_id 的结果,请使用
INNER JOIN
。请也发布您的视图脚本。
DO a
JOIN
between rentals and Rental_View using customer_id.If you only want to include results that have a customer_id in both tables, use an
INNER JOIN
.Please post your View' script as well.