DB2SQL查找第一个和最后一个雇用日期与Deptno10相同的部门编号,并计算年份

DB2SQL查找第一个和最后一个雇用日期与Deptno10相同的部门编号,并计算年份,sql,join,count,db2,aggregate-functions,Sql,Join,Count,Db2,Aggregate Functions,查找与第10部门同年雇用的部门。假设部门每年雇用10人。使用聚合函数和年份函数 这是我的SQL: select a.deptno, year(a.hiredate) as first, year(b.hiredate) as last, count (*) as num from (select d.deptno, h.hiredate, count(h.hiredate) from emp as d inner join emp as h on d.deptno=d.

查找与第10部门同年雇用的部门。假设部门每年雇用10人。使用聚合函数和年份函数

这是我的SQL:

  select a.deptno, year(a.hiredate) as first, year(b.hiredate) as last, count (*) as num
  from 
  (select d.deptno, h.hiredate, count(h.hiredate)
  from emp as d
  inner join emp as h on
  d.deptno=d.deptno
  where d.deptno = 10 
  group by d.deptno, h.hiredate
  having count (d.deptno)>1
  order by h.hiredate asc
  limit 1) as a

  inner join 

  (select d.deptno, h.hiredate, count(h.hiredate)
  from emp as d
  inner join emp as h on
  d.deptno=h.deptno
  where d.deptno = 10 
  group by d.deptno, h.hiredate
  having count (d.deptno)>1
  order by h.hiredate desc
  limit 1) as b
  on a.deptno=b.deptno
  group by a.deptno, a.hiredate, b.hiredate;
这是我的SQL的结果:

 deptno  first  last  num
---------------------------
 10      1980   1982   1
我尝试过不同的方法,例如添加d.deptno=10和d.deptno10,但结果为空。以及如何从两个连接中获得正确的计数? 如果有人能帮助我,我会很高兴的

这是我的桌子,如果有人想测试它:

 create table emp(
 EMPNO int,
 ENAME varchar(10),
 JOB varchar(20),
 MGR varchar(20),
 HIREDATE date,
 SAL float,
 COM varchar(20),
 DEPTNO int
 );




insert into emp values (7839, 'KING', 'President', '-' , '1981-11-17', 5000.00,'-',10);
insert into emp values(7698, 'BLAKE', 'Manager', 7839, '1981-05-01', 2850.00, '-', 30);
insert into emp values(7782, 'CLARK', 'Manager', 7839, '1981-06-09', 2450.00,'-', 10);
insert into emp values(7756, 'JONES', 'Manager', 7839, '1981-04-02', 2975.00, '-', 20);
insert into emp values(7788, 'SCOTT', 'Analyst', 7566, '1987-04-19', 3000.00, '-', 20);
insert into emp values(7902, 'FORD', 'Analyst', 7566, '1982-02-26', 3000.00, '-', 20);
insert into emp values(7369, 'SMITH', 'Clerk', 7902, '1980-12-17', 800.00, '-', 20);
insert into emp values(7499, 'ALLEN', 'Salesman', 7698, '1981-02-20', 1600.00, 300.00, 30);
insert into emp values(7521,'WARD', 'Salesman', 7698, '1981-02-22', 1250.00, 500.00, 30);
insert into emp values(7654, 'MARTIN', 'Salesman', 7698, '1981-09-28', 1250.00, 1400.00, 30);
insert into emp values(7844, 'TURNER', 'Salesman', 7698, '1981-09-08', 1500.00, 0.00, 30);
insert into emp values(7876, 'ADAMS', 'Clerk', 7798, '1987-05-23', 1100.00, '-', 20);
insert into emp values(7900, 'JAMES', 'Clerk', 7698, '1981-12-03', 950.00, '-', 30);
insert into emp values(7934, 'MILLER', 'Clerk', 7782, '1982-01-23', 1300.00, '-',10);
这是正确的结果,应该显示:

 deptno  first  last  num
---------------------------
 10      1981   1982   2
 20      1981   1982   2
如果我没有弄错,您可以使用一个通用表表达式来解决这个问题,该表达式计算每个部门的第一个和最后一个雇用年份,然后使用一个自联接来筛选与部门10具有相同的第一个和最后一个雇用日期的部门:

with depts as (
    select 
        deptno, 
        year(min(hiredate)) first_hire_year, 
        year(max(hiredate)) last_hire_year,
        count(*) total_hires
    from emp
    group by deptno
)
select d.*
from depts d
inner join depts d10
    on  d10.deptno = 10
    and d10.first_hire_year = d.first_hire_year
    and d10.last_hire_year = d.last_hire_year

另一方面,如果你想撤出所有部门,在第10部门确实雇佣了,也雇佣了的每一年,这是有点不同的。我将生成一份第10部门确实雇用员工的所有年份的列表,将其与部门列表合并,然后删除不匹配的部门,如下所示:

select 
    d.deptno, 
    min(y.hire_year) first_hire_year,
    max(y.hire_year) last_hire_year,
    count(d.deptno) count_hire_year
from 
    (select distinct year(hiredate) hire_year from emp where deptno = 10) y
    cross join (select distinct deptno from emp) d
    left join (select distinct deptno, year(hiredate) hire_year from emp) e
        on  e.hire_year = y.hire_year
        and e.deptno = d.deptno
group by d.deptno
having count(d.deptno) = count(e.deptno)
在这个中,第二个查询生成:

DEPTNO | FIRST_HIRE_YEAR | LAST_HIRE_YEAR | COUNT_HIRE_YEAR -----: | --------------: | -------------: | --------------: 10 | 1981 | 1982 | 2 20 | 1981 | 1982 | 2 部门|第一年|最后一年|统计年| -----: | --------------: | -------------: | --------------: 10 | 1981 | 1982 | 2 20 | 1981 | 1982 | 2
您好,我测试了您的代码,我仍然只得到了部门10的结果,其他被聘为部门10的部门都不见了。伯爵应该数年。我已经展示了结果应该是什么样子。我还没有学习表表达式,所以我不太确定是否允许我使用它们。@xamsas:请您编辑您的问题,提供一些示例数据,我们可以使用这些数据来测试查询,以及测试数据集的预期结果。是的,我已经这样做了,您可以看到我使用的表格和正确的结果@GMBThank you@xamsas。在您的数据集中,没有哪个部门的第一个和最后一个雇用年度与第10部门相同。第十部门有1981年和1982年,第二十部门有1980年和1987年,第三十部门只有1981年。谢谢@专线小巴