I am new to SQL. i am using Oracle10g. i have below query. it has 3 parts and am doing UNION of all three parts. But in every query most of the logic is common except join table. is it possible to avoid UNION and place everything in a single block?
SELECT DISTINCT e.some_id
FROM
main_table e,
main_table_join_one x //only change
where e.some_id = x.some_id(+)
and (x.status in('A','I') or x.status is null)
and e.code='XYZ' and e.second_code in('XYZ','ABC')
UNION
SELECT DISTINCT ef.some_id
FROM
main_table ef,
main_table_join_two xf //only change
where ef.some_id = xf.some_id(+)
and (xf.status in('A','I') or xf.status is null)
and ef.code='XYZ' and ef.second_code in('XYZ','ABC')
UNION
SELECT DISTINCT eff.some_id
FROM
main_table eff,
main_table_join_three xff //only change
where eff.some_id = xff.some_id(+)
and (xff.status in('A','I') or xff.status is null)
and eff.code='XYZ' and eff.second_code in('XYZ','ABC')
Thanks!
you can use exists
select distinct e.some_id
from main_table e
where e.code = 'XYZ'
and e.second_code in ('XYZ', 'ABC')
and (exists (select 0
from main_table_join_one x / / only change
where x.some_id = e.some_id
and (x.status in ('A', 'I') or x.status is null))
or exists
(select 0
from main_table_join_two x / / only change
where x.some_id = e.some_id
and (x.status in ('A', 'I') or x.status is null))
or exists
(select 0
from main_table_join_three x / / only change
where x.some_id = e.some_id
and (x.status in ('A', 'I') or x.status is null)))
EDITED
fully same result as in topic starter case
select distinct e.some_id
from main_table e
where e.code = 'XYZ'
and e.second_code in ('XYZ', 'ABC')
and (exists (select 0
from main_table_join_one x
where x.some_id = e.some_id
and x.status in ('A', 'I'))
or not exists
(select 0 from main_table_join_one x where x.some_id = e.some_id)
or exists
(select 0
from main_table_join_two x
where x.some_id = e.some_id
and x.status in ('A', 'I'))
or not exists
(select 0 from main_table_join_two x where x.some_id = e.some_id)
or exists
(select 0
from main_table_join_three x
where x.some_id = e.some_id
and x.status in ('A', 'I'))
or not exists
(select 0 from main_table_join_three x where x.some_id = e.some_id))
Why are you using an OUTER JOIN? You are only selecting the main_table.some_id
, and the outer joins mean you'll get that regardless of whether there's any matches in the other tables. (Although as Mark Bannister observes, because you've mangeled the outer join syntax you're actually running inner joins).
So you'll get the same result set (assuming you corrected the outer join syntax) if you just run:
SELECT DISTINCT e.some_id
FROM main_table e
where e.code='XYZ'
and e.second_code in('XYZ','ABC')
Perhaps you've mangled the logic in presenting us with a sanitized test case? Or perhaps you're not clear about the business rules you're supposed to be implementing?
select distinct ear.RecId,0 Recid,em.EmpCode EmpId,
(em.EmpFirstName+' '+em.EmpMiddleName+' '+em.EmpLastName) as Name,
dm.DeptName as Department,
EAR.AttendType [AttendType],
Convert(varchar,ActualDate,110) [AttendDate],
EAR.Reason,
ear.StatusOfAdmin Status
from Employeemaster em,
DepartmentMaster dm,
EmpAttenednaceRequest ear,
empAttendanceRequestTransaction eart
Try this query
SELECT DISTINCT e.some_id
FROM
main_table e,
main_table_join_one x,
main_table_join_two xf,
main_table_join_three xff
where e.code='XYZ' and e.second_code in('XYZ','ABC') AND
((e.some_id = x.some_id
and (x.status in('A','I') or x.status is null)
OR (ef.some_id = xf.some_id
and (xf.status in('A','I') or xf.status is null))
OR (eff.some_id = xff.some_id
and (xff.status in('A','I') or xff.status is null)))
If ef and eff does not exist
EDITED
SELECT DISTINCT e.some_id
FROM
main_table e,
main_table_join_one x,
where e.code='XYZ' and e.second_code in('XYZ','ABC') AND
e.some_id = x.some_id and
(x.status in('A','I') or x.status is null);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.