How can I avoid hard-coding this where clause in the inner select?
This is extremely fast, but I have to hard-code the inner-most where clause, and I obviously can't do that. I know how to work-around it by creating a function that takes the CUSTOMER_ID and returns the ORDER_ID from the most recent payment, and that's reasonably fast, but I thought it would be worthwhile to see if there was a way to do this in straight SQL. I also know that better design could make the problem go away.
Very much appreciate it if you could take a look and let me know if there's any way to get this kind of performance without hard-coding.
Thanks,
create or replace view customer_view as
select customer.customer_id,
customer.customer_name,
(
select t.order_id
from (
select payment.order_id
from payment
where payment.customer_id = 1 -- <-- Here's the line where I'm hard-coding the customer_id. Is there any way to reference the customer_id without hard-coding this?
order by payment.payment_date desc
) t
where rownum = 1
) as latest_order_id
from customer
;
select * from customer_view where customer_id = 1; <-- I want that inner-most select to use this customer_id, without having to hard-code it.