Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How can I avoid hard-coding this where clause in the inner select?

mattknowlesNov 16 2011 — edited Nov 16 2011
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.
This post has been answered by Peter Gjelstrup on Nov 16 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 14 2011
Added on Nov 16 2011
2 comments
246 views