0

我有以下问题:

为每个地点城市选择最早来的员工,显示地点城市和即将到来的日期

我试过关注一个

select  e.hiredate  as LOC,d.loc  as COMDATE
from    emp e, dept  d
where   e.hiredate in (
    select  min(e.hiredate) 
    from  emp e
    group by d.loc
)

它显示以下结果

17-DEC-80   NEW YORK
17-DEC-80   DALLAS
17-DEC-80   CHICAGO
17-DEC-80   BOSTON

但问题是只有一个 17-DEC-80,所以这意味着它会重复自己,我该如何解决?

4

3 回答 3

1

结果是来自 的产品cross join,您需要提供表之间的关系。前任

SELECT  e.hiredate AS LOC,
        d.loc AS COMDATE
FROM    emp e
        INNER JOIN dept d 
            ON e.deptno = d.deptno
WHERE   e.hiredate IN 
        (
            SELECT min(e.hiredate)
            FROM emp e
            GROUP BY d.loc
        )
于 2013-02-05T15:05:34.513 回答
1

如果您需要选择其他字段以及雇用日期/位置,您可以使用rank()分析:

select *
  from (select e.*, d.loc, rank() over (partition by d.loc order by e.hiredate) rnk
          from emp e
               inner join dept d
                       on d.deptno = e.deptno
       )
 where rnk = 1;

如果您只需要雇用日期和位置,那么只需使用min()

select min(e.hiredate), d.loc
  from emp e
       inner join dept d
               on d.deptno = e.deptno
 group by d.loc;
于 2013-02-05T15:12:38.520 回答
1

您的查询中有交叉联接或笛卡尔积,因为未联接 emp 和 dept 表。JW 的第一个查询的简单版本。并且别名不正确......:

select  e.hiredate  as LOC,d.loc  as COMDATE
 from    scott.emp e, scott.dept  d
 where e.deptno= d.deptno -- Cartesian if you not join --
 and e.hiredate in (
     select  min(e.hiredate) 
      from  scott.emp e
      group by d.loc
   )
  /
于 2013-02-05T15:13:30.633 回答