使用ANSI Oracle重新编写Oracle SQL

时间:2013-08-19 06:32:42

标签: sql oracle oracle10g

我是oracle和sql的新手,我想知道如何使用Oracle ANSI连接条件重新编写以下sql查询。

  SELECT     emp_no,
    (SELECT   emp_title
       FROM   hr_v_employee
       WHERE         organization_code LIKE
           SUBSTR (emp.depart_code, 0, 4) || '00'
             AND emp_position_code =
                (SELECT     MIN (emp_position_code)
                        FROM     hr_v_employee
                                WHERE     organization_code LIKE
                                    SUBSTR (emp.depart_code,
                                   0,
                                   4
                                   )
                 || '00'))
                 || ' '
                 || NVL (employee_deptartment, '-')
                     employee_deptartment
        FROM     employees e, employee_details o
      WHERE     emp.emp_no = o.emp_no(+)

编辑1

这是我试过的,我的问题是可以使用join或ANSI标准进行子查询吗?

 SELECT     emp_no,
        (SELECT   emp_title
           FROM   hr_v_employee
           WHERE         organization_code LIKE
               SUBSTR (emp.depart_code, 0, 4) || '00'
                 AND emp_position_code =
                    (SELECT     MIN (emp_position_code)
                            FROM     hr_v_employee
                                    WHERE     organization_code LIKE
                                        SUBSTR (emp.depart_code,
                                       0,
                                       4
                                       )
                     || '00'))
                     || ' '
                     || NVL (employee_deptartment, '-')
                         employee_deptartment
            FROM     employees e left outer join employee_details o (emp.emp_no = o.emp_no)

1 个答案:

答案 0 :(得分:1)

除了LEFT OUTER JOIN缺少ON关键字之外,您的查询应该没问题:

SELECT emp_no,
       (SELECT emp_title
          FROM hr_v_employee
         WHERE organization_code LIKE SUBSTR(emp.depart_code,
                                             0,
                                             4) || '00'
           AND emp_position_code =
               (SELECT MIN(emp_position_code)
                  FROM hr_v_employee
                 WHERE organization_code LIKE SUBSTR(emp.depart_code,
                                                     0,
                                                     4) || '00')) || ' ' ||
       NVL(employee_deptartment,
           '-') employee_deptartment
  FROM employees e
  LEFT OUTER JOIN employee_details o
    ON emp.emp_no = o.emp_no

请注意,您的列名称中有拼写错误 - 应将其命名为 employee_department ,而不是 employee_deptartment