我有两张桌子:
clients(client_id, f_name, l_name)
cars(car_id,model,price,..)
reservation(res_id, client_id_fk,by_date, up_to_date, car_id_fk)
现在我需要显示预留车的帐单(F_name | L_name | price* number of days
)
我试图做到这一点,但我得到错误:
ORA-00936:缺少表达
这是我尝试的方式:
select f_name,
l_name,
sum(cars.price*(date(reservation.up_to_date)-date(reservation.by_date)))
from clients
join reservation
where reservation.client_id_fk=clients.client_id
您将如何在Oracle SQl数据库中执行此操作?
答案 0 :(得分:1)
您必须将三张桌子连在一起。然后,您只需通过减去日期来计算日期差异。我们在这几天添加一个,因为当两个日期相等时你想要收费1天,而不是0。
select
clients.f_name || ' ' || clients.l_name || ' ' ||
to_char(cars.price * (reservation.up_to_date - reservation.by_date + 1), '$99,990.99')
from
clients
inner join reservation
on clients.client_id = reservation.client_id_fk
inner join cars
on reservation.car_id_fk = cars.car_id
请参阅:Format Models了解数字格式。
您还可以为表名使用别名,以便更简洁:
select
c.f_name || ' ' || c.l_name || ' ' ||
to_char(cars.price * (r.up_to_date - r.by_date + 1), '$99,990.99')
from
clients c
inner join reservation r
on c.client_id = r.client_id_fk
inner join cars
on r.car_id_fk = cars.car_id