0

我有一张表 employee(id,dept_id,salary,hire_date,job_id) 。我必须执行以下查询。

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

我已经完成了查询,但我无法得到至少。请检查是否正确。

          select id, WEEKDAY(hire_date)+1 as days,count(WEEKDAY(hire_date)+1) as count

           from test.employee  group by days
4

5 回答 5

1
select id from test.employee where hire_date in
    ( select count(id) count,hire_date 
      from test.employee
      order by count desc 
      limit 1)

这应该工作

于 2012-11-26T05:13:25.580 回答
1

这应该会为您提供雇用最少员工的工作日:

SELECT 
   count(id) as `Total`,
   WEEKDAY(hire_date) as `DoW`
FROM
   test.employee
GROUP BY `DoW`
ORDER BY `Total` DESC LIMIT 1;
于 2012-11-26T05:09:00.447 回答
1

您可以尝试这样做,因为如果您有多个工作日雇用相同数量的最少员工,它不会限制为一条记录。实际上这是有道理的。以下是基于样本数据。

询问:

-- find minimum id count
SELECT MIN(e.counts) INTO @min 
FROM (SELECT COUNT(*) as counts,
      WEEKDAY(hire_date+1) as day 
FROM employee
GROUP BY WEEKDAY(hire_date+1)) e
;

-- show weekdays with minimum id counts
SELECT e2.counts as mincount, 
WEEKDAY(e1.hire_date+1) as weekday
FROM employee e1
  JOIN (SELECT COUNT(id) as counts, 
        WEEKDAY(hire_date+1) as day
        FROM employee 
        GROUP BY day
        HAVING COUNT(*) = @min) e2
    ON WEEKDAY(e1.hire_date+1) = e2.day;

结果:

MINCOUNT    WEEKDAY
1           6
1           3
1           4
1           2
于 2012-11-26T06:52:04.977 回答
0
select min(id), WEEKDAY(hire_date)+1 as days,count(WEEKDAY(hire_date)+1) as count

       from test.employee  group by days
于 2012-11-26T05:00:03.140 回答
0
       SELECT
           *
         FROM
       employee
       WHERE
          DAYOFWEEK(hire_date)
         IN 
      (
             SELECT 
            weekday 
        FROM
           (
             SELECT 
        count(*) as bcount, 
        DAYOFWEEK(hire_date) as weekday 
    FROM 
        employee as a
    GROUP BY 
        weekday
    HAVING
        bcount = (
            SELECT 
                MIN(tcount)
            FROM
                (
                    SELECT 
                        count(*) as tcount, 
                        DAYOFWEEK(hire_date) as weekday 
                    FROM 
                        employee 
                    GROUP BY 
                        weekday

                ) as t
        )
    ) as q
于 2012-11-26T09:53:24.863 回答