Ganesh wrote:
Thats right, but my WHERE criteria will make sure rest of filtering but for now I get the error message no matter whatever I do!!!...
Relation at index 2 doesn't contain an entity already added to the FROM clause. Bad alias?
Here is my WHERE criteria...
where veh_viol_incd_dtl.sa_vp_cde = sa_vp_cde and veh_viol_incd_dtl.vp_num = vp_num and veh_viol_incd_dtl.id_num = id_num_vp_holder and veh_viol_incid.incid_dte is not null and veh_viol_incd_dtl.veh_viol_type is not null and ((stud_life_config.show_on_web = 'G' and veh_viol_incd_dtl.viol_access_rstr = 'G') or (stud_life_config.show_on_web = 'R' and veh_viol_incd_dtl.viol_access_rstr = 'R') or stud_life_config.show_on_web = 'B')
If I run the entire SQL in MSS, I don't get any error message and it works fine!
yes, but it doesn't make sense.
->
SELECT DISTINCT veh_viol_types.veh_viol_desc, veh_viol_incid.incid_dte, veh_viol_incd_dtl.fine_amt
FROM veh_viol_incd_dtl JOIN veh_viol_incid ON veh_viol_incd_dtl.incid_num = veh_viol_incid.incid_num LEFT OUTER JOIN veh_viol_types ON veh_viol_incd_dtl.veh_viol_type = veh_viol_types.veh_viol_type JOIN stud_life_config ON sub_module_cde = 'VM'
will
join each row resulting from
veh_viol_incd_dtl JOIN veh_viol_incid ON veh_viol_incd_dtl.incid_num = veh_viol_incid.incid_num LEFT OUTER JOIN veh_viol_types ON veh_viol_incd_dtl.veh_viol_type = veh_viol_types.veh_viol_type
with all stud_life_config rows where sub_module_cde = "VM".
Then, you're filtering rows out, but that doesn't affect the rows already returned formed by:
veh_viol_incd_dtl JOIN veh_viol_incid ON veh_viol_incd_dtl.incid_num = veh_viol_incid.incid_num LEFT OUTER JOIN veh_viol_types ON veh_viol_incd_dtl.veh_viol_type = veh_viol_types.veh_viol_type
as it's typically a cross join.
It will result in a lot of duplicate rows. The main issue is: you don't select any data from stud_life_config, so:
1) it doesn't affect the outcome of the query, as the resultset is not selected from stud_life_config
2) filtering on it won't limit the resultset as every row in stud_life_config is joined with every row in the data of which the resultset is retrieved.
Typical example:
select distinct c.*
from customers c inner join orders o
on c.customerid = o.customerid
-- join products p on p.categoryid=2
--where p.Supplierid=3
Now, if I uncomment the last 2 rows, do you think it will affect the outcome? No, because it won't limit any results from the join from which the resultset is retrieved.
Try it, remove all filters on stud_life_config frm the where clause and the clause from the join, it should result in the same resultset.