在自己的子查询上连接表

时间:2013-04-19 19:25:17

标签: sql sql-server subquery

我正在尝试更好地理解子查询,并且我在下面的这个脚本中遇到了问题。 (SQL Server数据库)

SELECT division_name, business_name, assignment_code, assignment_desc, last_name, first_name, total_salary, assign_fte, birth_year
  FROM table1 join
  (SELECT division_name, last_name, first_name, assignment_code, emp_id 
  FROM table1) raw
   WHERE raw.year_time = year_time
  AND division_name <> raw.division_name
  AND last_name = raw.last_name
  AND first_name = raw.first_name
    ORDER BY last_name, first_name

我需要做的是加入同一个表并提取员工为两个不同部门工作的数据。上面的代码在where子句附近有语法错误。

1 个答案:

答案 0 :(得分:1)

你错过了JOIN的ON部分

...即

SELECT division_name, business_name, assignment_code, assignment_desc, last_name, first_name, total_salary, assign_fte, birth_year
  FROM table1 join
  (SELECT division_name, last_name, first_name, assignment_code, emp_id 
  FROM table1) raw ON raw.year_time = year_time
   WHERE
   division_name <> raw.division_name
  AND last_name = raw.last_name
  AND first_name = raw.first_name
    ORDER BY last_name, first_name