别名子查询不起作用

时间:2013-12-08 17:54:23

标签: sql oracle subquery

我有以下代码:

SELECT
CU.customer_ID AS "Customer ID",
CU.cust_LNAME AS "Last Name",
CU.cust_FNAME AS "First Name",
RH.movie_ID AS "Movie ID",
MO.movie_title AS "Movie Title",
MO.release_year AS "Release Year",
(
SELECT COUNT(*) FROM RentalHistory RH2 WHERE RH2.movie_ID = RH.movie_ID AND RH2.customer_ID = RH.customer_ID HAVING COUNT(*) > 1
) "Nbr. Of Times Rented"
FROM Customer CU
RIGHT JOIN Rentalhistory RH ON (RH.customer_ID = CU.customer_ID)
INNER JOIN Movie MO ON (MO.movie_ID = RH.movie_ID)
GROUP BY CU.customer_ID, RH.movie_ID    
HAVING "Nbr. Of Times Rented" > 1
ORDER BY CU.cust_LNAME ASC, CU.cust_FNAME ASC;

我一直在获取别名子查询的无效标识符?关于我做错了什么的想法?

1 个答案:

答案 0 :(得分:0)

我认为你不需要子查询。这样做你想要的吗?

SELECT CU.customer_ID AS "Customer ID",
       CU.cust_LNAME AS "Last Name",
       CU.cust_FNAME AS "First Name",
       RH.movie_ID AS "Movie ID",
       MO.movie_title AS "Movie Title",
       MO.release_year AS "Release Year",
       COUNT(*) as "Nbr. Of Times Rented"
FROM Customer CU RIGHT JOIN
     Rentalhistory RH
     ON RH.customer_ID = CU.customer_ID INNER JOIN
     Movie MO
     ON MO.movie_ID = RH.movie_ID
GROUP BY CU.customer_ID, RH.movie_ID    
HAVING count(*) > 1
ORDER BY CU.cust_LNAME ASC, CU.cust_FNAME ASC;