Question : Oracle SQL Query

I need to join two tables T1 and T2 together on a common column "id" which I can do....

The relationship is a one-to-many with T1 being the 1 side and T2 being the many...

T1 = id

T2 = id , secidtype , secid

On the UI the user can set three choices "secidtype" which define the order with which a search should be carried out on T2 to find a match on "secidtype" which may not exist....

For the three options set if the first choice does not exist the search on T2 should begin again for choice 2. It is assumed one out of the three choices set on the UI does exist in T2.  

The first choice to be matched in T2 on "secidtype" set by the user on the UI then the column "secid" should be returned...

How do I code this in one SQL statement possible with a "with" statement..? ( SQL only please )

Regards Ian  


Answer : Oracle SQL Query

select t2.secid
from
(
select row_number() over (order by
  case when param1=t2.secidtype then 1 else 2 end,
  case when param2=t2.secidtype then 1 else 2 end) rn, t1.id, t2.secid
from t1 inner join t2 on t1.id=t2.id
where t2.secidtype in (param1, param2, param3)
) sq
where rn = 1

The question is what role does t1 play in your question that only deals with t2??
I have included it in the inner join just as an example.
Random Solutions  
 
programming4us programming4us