SQL help

General
Enthusiast

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

3 REPLIES
Enthusiast

Re: SQL help

can someone help me with the logic

Enthusiast

Re: SQL help

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

Junior Contributor

Re: SQL help

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