Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Multiple column case/subselect advice
Geoff Muldoon wrote:
> damorgan_at_psoug.org says... >
> >> Show us what you've done. > > As mentioned, I have a *rough* working solution, but am looking for a more > efficient technique, one that ideally won't need to use the case statement > and nested sub-selects three times, once for each of the affected columns. > > Current work: > > select a.ITEM_ID, > nvl((case when a.ITEM_STATUS = 'PL' then --first condition met > (select b.ITEM_STAGE --get value from history table instead > from ITEM_STATUS_HIST b > where b.ITEM_ID = a.ITEM_ID > and b.HIST_NO = --pick row in history table > (select max(c.HIST_NO) --get the max ... > from ITEM_STATUS_HIST c > where c.ITEM_ID = a.ITEM_ID > and c.HIST_NO < --... that is less that the last > (select max(d.HIST_NO) > from ITEM_STATUS_HIST d > where d.ITEM_ID = a.ITEM_ID)) > and b.ITEM_STATUS = 'WD') --second condition, else will be null > else null end), --else first condition not met, set as null > a.ITEM_STAGE) --nvl value if either condition not met > as ITEM_STAGE--, > --<repeat above logic for ITEM_STATUS and EFFECT_DATE columns>, > --a.<other columns> > from ITEM_INFO a > > Why am I doing this? It is a change to part of a data warehouse ETL > stored proc, necessitated by an application script <cough/splutter> > "enhancement". > > Geoff M
If you are, as it appears, repeating the same subquery multiple times
look into using the WITH CLAUSE:
http://www.psoug.org/reference/with.html
as one possible solution.
-- Daniel A. Morgan University of Washington damorgan_at_x.washington.edu (replace x with u to respond) Puget Sound Oracle Users Group www.psoug.orgReceived on Mon Dec 04 2006 - 17:32:49 CST
![]() |
![]() |