查找最受欢迎的汽车数量和已租用的汽车总数

时间:2019-03-18 18:56:54

标签: oracle plsql

我正在尝试从给定的数据以及租车总天数中找到最受欢迎的租车。 我已经编写了PL / SQL代码来完成这项工作

create or replace procedure CarRentalSiteDetail (id IN CarRentalSite.CarRentalSiteId%TYPE) as

rental_name CarRentalSite.CarRentalSiteName%TYPE;
rental_city CarRentalSite.City%TYPE;
num number;
pop number;
c_name Car.CarName%TYPE;
total number;

BEGIN

Select CarRentalSite.CarRentalSiteName into rental_name from CarRentalSite where CarRentalSiteId = id; 
Select CarRentalSite.City into rental_city from CarRentalSite where CarRentalSiteId = id; 
Select count(Rentals.CarRentalSiteId) into num from CarRentalSite INNER JOIN Rentals ON CarRentalSite.CarRentalSiteId = Rentals.CarRentalSiteId where CarRentalSite.CarRentalSiteId = id Group by CarRentalSite.CarRentalSiteId;
Select CarId into pop from (Select CarId, count(count) as c from Rentals Group By CarId Order By c desc) where rownum = 1;
Select Car.CarName into c_name from Car where CarId = pop;
Select t into total from (Select CarId, sum(count) as t from Rentals Group By CarId) s where s.CarId = pop;

dbms_output.PUT_line(CONCAT('CarRentalSite Name: ',rental_name));
dbms_output.PUT_line(CONCAT('CarRentalSite City: ',rental_city));
dbms_output.PUT_line(CONCAT('CarRentalSite Total Rentals: ',num));
dbms_output.PUT_line(CONCAT('Most Popular Compact Car: ',c_name));
dbms_output.PUT_line(CONCAT('Total Days Rented: ', total));

END CarRentalSiteDetail;
/

show errors;

BEGIN
CarRentalSiteDetail(1);
end;
/

我在逻辑上写的内容对我来说很有意义,应该可以完成工作,但是它会返回此错误,我无法解决

15/1     PL/SQL: SQL Statement ignored
15/49    PL/SQL: ORA-00904: "COUNT": invalid identifier
17/1     PL/SQL: SQL Statement ignored
17/45    PL/SQL: ORA-00904: "COUNT": invalid identifier

1 个答案:

答案 0 :(得分:2)

count是Oracle DB(在大多数SQL服务器中)的保留字,您不能将其用作列名或变量名。

尝试更改它,您不会收到此错误