All Forums General
Radsubra2003 10 posts Joined 05/14
06 May 2014
SQL help

Iam new to Teradata and I need some SQL help for the below logic, since I need to put this
in one SQL and implement in PDO
I need get mtr_ID from one table A but if that join returns more than one row or no match found then
I need to join to another table and get the mtr_id
below is the logic
Select  a. mtr_id from  table A Where   A.id  = STGtable. ID
If no rows returned or more than one MTR_IDs returned from the above logic, then do this:
select  mtr_id from  table B  Where  B.ID = STGtable.ID

Radsubra2003 10 posts Joined 05/14
06 May 2014

can someone help me with the logic

Raja_KT 1246 posts Joined 07/09
06 May 2014

Can you try on bteq with this logic?
Select  a. mtr_id from  table A Where   A.id  = STGtable. ID
.if activitycount =... <> ........ then .goto  .label l123 say( or do something)
.label l123
do something
 

Raja K Thaw
My wiki: http://en.wikipedia.org/wiki/User:Kt_raj1
Street Children suffer not by their fault. We can help them if we want.

dnoeth 4628 posts Joined 11/04
06 May 2014

You might try a conditional join based on the result of the first join, e.g.

SELECT
   id, COALESCE(a.mtr_id, b.mtr_id)
FROM
 (
   SELECT
      STGtable.ID, 
      CASE WHEN MIN(a.mtr_id) = MAX(a.mtr_id) THEN MIN(a.mtr_id) END AS mtr_id
   FROM STGtable LEFT JOIN TableA AS a 
   ON A.id  = STGtable. ID
   GROUP BY 1
) AS a
LEFT JOIN TableB AS b
ON B.ID = a.ID
AND a.mtr_id IS NULL

 

Dieter

You must sign in to leave a comment.