题目如下:
一个企业信息表company,有如下数据。
主建(id) 部门编号(dept_no) 部门名称(name) 修改时间(chanage_date)
1 101 财务部 2008/07/04
2 10101 财务结算部 2005/05/06
3 10102 财务管理部 2008/08/09
4 102 人事部 2005/06/04
5 10201 公关部 2006/05/08
6 101 财务公司 2010/08/08
7 10101 财务结算公司 2010/10/10
8 10201 人力资源咨询公司 2009/09/09
9 103 研发部 2006/05/07
10 103 研发中心亚洲分院 2009/11/10
11 104 市场部 2009/02/21如上数据。由于公司发展壮大,公司将财务部门名称进行了修改。
查询 部门编号,最新的部门名称,最后修改的时间 ,用SQL语句实现。
查询结果为:
101 财务公司 2010/08/08
102 人事部 2005/06/04
10101 财务结算公司 2010/10/108
10201 人力资源咨询公司 2009/09/09
103 研发中心亚洲分院 2009/11/10
104 市场部 2009/02/21
10102 财务管理部 2008/08/09
一个企业信息表company,有如下数据。
主建(id) 部门编号(dept_no) 部门名称(name) 修改时间(chanage_date)
1 101 财务部 2008/07/04
2 10101 财务结算部 2005/05/06
3 10102 财务管理部 2008/08/09
4 102 人事部 2005/06/04
5 10201 公关部 2006/05/08
6 101 财务公司 2010/08/08
7 10101 财务结算公司 2010/10/10
8 10201 人力资源咨询公司 2009/09/09
9 103 研发部 2006/05/07
10 103 研发中心亚洲分院 2009/11/10
11 104 市场部 2009/02/21如上数据。由于公司发展壮大,公司将财务部门名称进行了修改。
查询 部门编号,最新的部门名称,最后修改的时间 ,用SQL语句实现。
查询结果为:
101 财务公司 2010/08/08
102 人事部 2005/06/04
10101 财务结算公司 2010/10/108
10201 人力资源咨询公司 2009/09/09
103 研发中心亚洲分院 2009/11/10
104 市场部 2009/02/21
10102 财务管理部 2008/08/09
from company a
where not exists(
select 1 from company where id = a.id and chanage_date>a.chanage_date
)
select id ,dept_no,name,chanage_date,row_number()over(partition by dept_no order by chanage_date desc) rn from company
)
where rn=1;
from aa a,
(select dept_no,max(vdate) vdate from aa group by dept_no )b
where a.dept_no=b.dept_no and a.vdate=b.vdate
1 2 3 4 ...
rank()
1 2 2 4 ...
dense_rank()
1 2 2 3 ...
select dept_no,name,chanage_date
from
(select dept_no,name,chanage_date,row_number() over(partition by dept_no order by chanage_date desc) rn
from company) a
where a.rn=1--or
select dept_no,name,chanage_date
from company a
where not exists(select 1 from company b where a.dept_no=b.dept_no and a.chanage_date<b.chanage_date)
update company set name='财政公司',chang_data='2010/8/8' where id=101;
其他类似