1

显示在一周中雇用最多员工的那一天雇用的所有员工的姓名。

桌子:

Steven  06/17/1987
Neena   09/21/1989
Lex     01/13/1993
Alex    01/03/1990
Bruce   05/21/1991
Diana   02/07/1999
Kevin   11/16/1999
Trenna  10/17/1995
Curtis  01/29/1997
Randall 03/15/1998
Peter   07/09/1998
Eleni   01/29/2000
Ellen   05/11/1996
Jonath  03/24/1998
Kimber  05/24/1999
Jenni   09/17/1987
Michael 02/17/1996
Pat     08/17/1997
Shelley 06/07/1994
William 06/07/1994

到目前为止我所拥有的。

SELECT FIRST_NAME, to_char(hire_date,'d') AS DOW FROM EMPLOYEES;

Steven  4
Neena   5
Lex     4
Alex    4
Bruce   3
Diana   1
Kevin   3
Trenna  3
Curtis  4
Randall 1
Peter   5
Eleni   7
Ellen   7
Jonath  3
Kimbe   2
Jenni   5
Michael 7
Pat     1
Shelley 3
William 3

星期天是 1,星期一是 2,……以此类推……

现在我需要选择具有最大重复次数的那个。

通过查看表格,我们将知道它是 3(星期二)。我知道我需要使用子查询来获取它,它有吗?

4

3 回答 3

4

我倾向于为此使用分析函数:

select e.*
from (SELECT to_char(hire_date, 'd') AS DOW, count(*) as cnt,
             row_number() over (order by count(*) desc) as seqnum
      FROM EMPLOYEES
     ) dow join
     EMPLOYEEs e
     on dow.DOW = to_char(e.hire_date, 'd') and seqnum = 1;
于 2013-09-11T01:09:59.013 回答
1
select * 
  from employees
 where to_char(hire_date, 'd') = (
     select max(to_char(hire_date, 'd')) keep (dense_rank last order by count(*))
       from employees 
      group by to_char(hire_date, 'd')
 );

SQLFiddle

于 2013-09-11T01:51:19.030 回答
1

一种方法,在上面扩展您的查询(SQL Fiddle Example):

SELECT FIRST_NAME, to_char("hire_date", 'd') AS DOW
FROM EMPLOYEES
WHERE to_char("hire_date", 'd') =
  (
      SELECT b.DOW 
      FROM
      (
        select a.*, ROWNUM rnum 
        from (
          SELECT to_char("hire_date", 'd') AS DOW, COUNT(1) AS cnt
          FROM EMPLOYEES 
          GROUP BY to_char("hire_date", 'd')
          ORDER BY cnt DESC
        ) a
        where rownum = 1
      ) b
  )
于 2013-09-11T00:41:47.350 回答