I am re-writing a query that was generated by Business Objects and uses “old-fashioned” implicit join syntax. The code joins a table to itself in one part and also has a “global” where clause. For example:
select a.col1
, b.col2
from MYDB.TABLE a, MYDB.TABLE b
where a.something=b.something_else
and MYDB.TABLE.source='A'
Above is a made up illustration of the question. The actual query is very long, joining about ten tables.
My question: as written above, does the “extra” where condition apply to both instances of the same table? I think yes, but I’ve never seen code like this before. I’m using Teradata but I think this is a general SQL question.
UPDATE: Perhaps my attempt to narrow the question wasn’t accurate. Here is the complete query I’m trying to modify:
SELECT
abs_contrct_prof.contrct_nbr_txt,
gbs_org_LVL1.bus_pln_sgmnt_cd,
abs_gnrc_lst_of_val_LVL1.wirls_val_1_txt
FROM
EDWABSUSERVIEWS.abs_contrct abs_contrct_prof,
EDWABSUSERVIEWS.gbs_org gbs_org_LVL1,
EDWABSUSERVIEWS.abs_gnrc_lst_of_val abs_gnrc_lst_of_val_LVL1,
EDWABSUSERVIEWS.abs_contrct,
EDWABSUSERVIEWS.gbs_sls_actv_blng_org_rltd,
EDWABSUSERVIEWS.gbs_sls_actv_org_rltd
WHERE
( abs_contrct_prof.type_cd = 'PROFILE' )
AND ( EDWABSUSERVIEWS.abs_contrct.type_cd = 'AGREEMENT' )
AND ( abs_contrct_prof.prnt_contrct_id=EDWABSUSERVIEWS.abs_contrct.contrct_id )
AND ( abs_contrct_prof.org_id=EDWABSUSERVIEWS.gbs_sls_actv_org_rltd.org_id )
AND ( EDWABSUSERVIEWS.gbs_sls_actv_org_rltd.gbs_lvl_3_org_id=EDWABSUSERVIEWS.gbs_sls_actv_blng_org_rltd.gbs_lvl_3_org_id )
AND ( EDWABSUSERVIEWS.gbs_sls_actv_blng_org_rltd.gbs_lvl_1_org_id = gbs_org_LVL1.org_id )
AND ( gbs_org_LVL1.bus_pln_sgmnt_cd=abs_gnrc_lst_of_val_LVL1.nm_txt and abs_gnrc_lst_of_val_LVL1.actv_ind = 'Y' and abs_gnrc_lst_of_val_LVL1.type_cd ='ABS_MOBILITY_SEGMENT' )
AND
abs_contrct_prof.contrct_nbr_txt IN @variable('FAN')
AND ( EDWABSUSERVIEWS.abs_contrct.sts_cd = 'Active' )
AND ( EDWABSUSERVIEWS.abs_contrct.type_cd='AGREEMENT' )
Note the table EDWABSUSERVIEWS.abs_contrct is referenced twice in the FROM clause, one time using an alias and once without. And yes, this query works as written, but I want to re-write it to use explict join syntax (as someone commented).
I ran an EXPLAIN on the query and it appears that the “extra” where conditions (for ‘Active’ and ‘AGREEMENT’) are in fact applied to both instances of the table separately.
If your
TRANSACTION MODEisTERADATAthe optimizer will product join the fully qualified table reference in theWHEREclause against the spool file containing the INNER JOIN results. I have not been able to test this yet withTRANSACTION MODEset toANSI.Example
Explain – Teradata Mode
UPDATE
Results
The filter condition was not applied to the tables in the
FROMclause.