0

我是 SQL 新手。我正在使用Oracle10g。我有以下查询。它有 3 个部分,并且正在对所有三个部分进行 UNION。但是在每个查询中,除了连接表之外,大多数逻辑都是常见的。是否可以避免 UNION 并将所有内容放在一个块中?

    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')

谢谢!

4

4 回答 4

1

你可以使用存在

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)))

已编辑

与主题启动案例中的结果完全相同

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))
于 2013-03-06T11:06:27.407 回答
0

为什么要使用外部联接?您只选择main_table.some_id,而外连接意味着无论其他表中是否有任何匹配项,您都会得到它。(尽管正如 Mark Ba​​nnister 所观察到的,因为您已经破坏了外部联接语法,您实际上是在运行内部联接)。

因此,如果您只运行以下命令,您将获得相同的结果集(假设您更正了外连接语法):

SELECT DISTINCT e.some_id
FROM main_table e
where e.code='XYZ' 
and e.second_code in('XYZ','ABC')

也许您在向我们展示经过清理的测试用例时已经破坏了逻辑?或者您可能不清楚您应该实施的业务规则?

于 2013-03-06T12:34:34.360 回答
0
    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  
于 2014-09-26T16:58:42.983 回答
-1

试试这个查询

 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)))

如果 ef 和 eff 不存在

已编辑

 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);
于 2013-03-06T09:44:44.993 回答