Author: AnandhaMuruganB
Posted: Wed Aug 03, 2016 6:55 pm (GMT 5.5)
Thank you Rohit. I have modified the query as below and tried to answer your questions. Please see if this will help.
SELECT
X.Cust, Y.Org_id, Y.Status
from
TABLEA X,
TABLEB y,
TABLEC z
WHERE
x.STATUS='ACT'
AND
X.Cust_id=Z.Cust_id
AND
Z.Org_id=Y.Org_id
AND
NOT EXISTS
-- Correlated subquery
(
SELECT
'1'
FROM
TABLEB Y1
where
Y1.Org_id=Y.Org_id
and
Y1.Status = 'ACT'
)
with ur;
Main Query:
SELECT
X.Cust, X.STATUS Y.Org_id, Y.Status
from
TABLEA X,
TABLEB y,
TABLEC z
WHERE
x.STATUS='ACT'
AND
X.Cust_id=Z.Cust_id
AND
Z.Org_id=Y.Org_id
1. code tags missing - Do you mean the code snippet from mainframe.
I am sorry, currently i do not have access to mainframes
2. wrong Alias names referenced all over - Thank you. I have corrected it now.
3. end_dt , the null condition is missing. - Requirement doesn't imply any filter condition based on END_DT column
It talks only about Status of the Cust_id and Org_id which is readily available in column Status of TableB. If the status has to checked up to date, then we have to modify accordingly
4. How do you ensure to block the cus_id if there is a even single stat 'ACT' per org_id? - Given below, I have tried to spilit the main query result and subquery result set
as i expected it to run. please correct if i am wrong.
Main query is expected to pull 7 rows in the result set by joining the 3 tables.
But for each row pulled from main query, sub query will be executed and "NOT EXISTS" clause check will be performed.
Ex: First 3 rows in the result has Y.Org_id as 22.
When the correlated sub query is run for this Org_id (22), it Exists since there is a row with status "ACT" in table B.
So, the "NOT EXISTS" clause fails and the row is not pulled to the final result set. Whereas "NOT EXISTS" clause passes for the 4th row and it will be reported to final output.
Main Query:
SELECT
X.Cust, X.STATUS Y.Org_id, Y.Status
from
TABLEA X,
TABLEB y,
TABLEC z
WHERE
x.STATUS='ACT'
AND
X.Cust_id=Z.Cust_id
AND
Z.Org_id=Y.Org_id
Result Set from Main Query:
X.Cust_id | X.Cust_Status | Y.Org_id | Y.Company_cd | Y.Status | Is the row Filtered with respect to Correlated subquery
123 ACT 22 AL INACT Yes, Filtered
123 ACT 22 AK ACT Yes, Filtered
123 ACT 22 AG SUSP Yes, Filtered
123 ACT 27 AG INACT No, Reported to the output
100 ACT 21 AG ACT Yes, Filtered
100 ACT 21 AK INACT Yes, Filtered
100 ACT 21 AK SUSP Yes, Filtered
SUB QUERY:
AND NOT EXISTS
-- Correlated subquery
(
SELECT
'1'
FROM
TABLEB Y1
where
Y1.Org_id=Y.Org_id
and
Y1.Status = 'ACT'
)
with ur;
_________________
Anand
"If you want to leave your footprint on the sand of time, do not drag your feet"
Posted: Wed Aug 03, 2016 6:55 pm (GMT 5.5)
Rohit Umarjikar wrote: | ||
Because there is no relationship. Anand, 1. code tags missing 2. wrong Alias names referenced all over. 3. end_dt , the null condition is missing. 4. How do you ensure to block the cus_id if there is a even single stat 'ACT' per org_id? |
Thank you Rohit. I have modified the query as below and tried to answer your questions. Please see if this will help.
SELECT
X.Cust, Y.Org_id, Y.Status
from
TABLEA X,
TABLEB y,
TABLEC z
WHERE
x.STATUS='ACT'
AND
X.Cust_id=Z.Cust_id
AND
Z.Org_id=Y.Org_id
AND
NOT EXISTS
-- Correlated subquery
(
SELECT
'1'
FROM
TABLEB Y1
where
Y1.Org_id=Y.Org_id
and
Y1.Status = 'ACT'
)
with ur;
Main Query:
SELECT
X.Cust, X.STATUS Y.Org_id, Y.Status
from
TABLEA X,
TABLEB y,
TABLEC z
WHERE
x.STATUS='ACT'
AND
X.Cust_id=Z.Cust_id
AND
Z.Org_id=Y.Org_id
1. code tags missing - Do you mean the code snippet from mainframe.
I am sorry, currently i do not have access to mainframes
2. wrong Alias names referenced all over - Thank you. I have corrected it now.
3. end_dt , the null condition is missing. - Requirement doesn't imply any filter condition based on END_DT column
It talks only about Status of the Cust_id and Org_id which is readily available in column Status of TableB. If the status has to checked up to date, then we have to modify accordingly
4. How do you ensure to block the cus_id if there is a even single stat 'ACT' per org_id? - Given below, I have tried to spilit the main query result and subquery result set
as i expected it to run. please correct if i am wrong.
Main query is expected to pull 7 rows in the result set by joining the 3 tables.
But for each row pulled from main query, sub query will be executed and "NOT EXISTS" clause check will be performed.
Ex: First 3 rows in the result has Y.Org_id as 22.
When the correlated sub query is run for this Org_id (22), it Exists since there is a row with status "ACT" in table B.
So, the "NOT EXISTS" clause fails and the row is not pulled to the final result set. Whereas "NOT EXISTS" clause passes for the 4th row and it will be reported to final output.
Main Query:
SELECT
X.Cust, X.STATUS Y.Org_id, Y.Status
from
TABLEA X,
TABLEB y,
TABLEC z
WHERE
x.STATUS='ACT'
AND
X.Cust_id=Z.Cust_id
AND
Z.Org_id=Y.Org_id
Result Set from Main Query:
X.Cust_id | X.Cust_Status | Y.Org_id | Y.Company_cd | Y.Status | Is the row Filtered with respect to Correlated subquery
123 ACT 22 AL INACT Yes, Filtered
123 ACT 22 AK ACT Yes, Filtered
123 ACT 22 AG SUSP Yes, Filtered
123 ACT 27 AG INACT No, Reported to the output
100 ACT 21 AG ACT Yes, Filtered
100 ACT 21 AK INACT Yes, Filtered
100 ACT 21 AK SUSP Yes, Filtered
SUB QUERY:
AND NOT EXISTS
-- Correlated subquery
(
SELECT
'1'
FROM
TABLEB Y1
where
Y1.Org_id=Y.Org_id
and
Y1.Status = 'ACT'
)
with ur;
_________________
Anand
"If you want to leave your footprint on the sand of time, do not drag your feet"