我正在尝试创建一个表格,其中包含每位客户最新的电影租赁日期。
目前我有一个涵盖大部分所需数据的视图,除了每个客户的每部电影的返回日期,此信息位于名为rental的表中,我无法在FROM部分中引用,因为它与视图。
这是我目前所做的,但我在某些表上一直收到无效的对象错误。例如出租和出租_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
不确定这里有什么问题,让第二个嵌入式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
答案 0 :(得分:0)
使用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
也请发布您的“查看”脚本。