MAX() 最新日期,无效对象

发布于 2024-12-14 06:13:04 字数 943 浏览 1 评论 0原文

我正在尝试创建一个表,其中包含每个客户的最新电影租赁日期。

目前,我有一个视图,涵盖了大部分所需数据,除了每个客户每部电影的返回日期之外,此信息位于一个名为“租金”的表中,我无法在“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-12-21 06:13:04

使用 customer_id 在租赁和 Rental_View 之间进行 JOIN
如果您只想包含两个表中都具有 customer_id 的结果,请使用 INNER JOIN

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 
INNER 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  

请也发布您的视图脚本。

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.

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 
INNER 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  

Please post your View' script as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文