日期:2014-05-18  浏览次数:20644 次

请教个简单sql语句
表A
员工号 培训时间
1 2
2 4
3 6
4 3
5 1 
6 1
表B
员工号 员工所属部门
1 办公室
2 办公室
3 客服
4 客服
5 开发
6 开发
要求最后查询出的结果:
部门 部门员工数 该部门员工参加培训的最少天数 该部门参加培训天数最少的人数
办公室 2 2 1
客服 2 3 1
开发 2 1 1

------解决方案--------------------
SQL code
select b.员工所属部门 部门 , min(a.培训时间) 该部门员工参加培训的最少天数
from a,b
where a.员工号=b.员工号

------解决方案--------------------
SQL code

create table A(id int,days int)
create table B(id int,department varchar(50))
insert into A
select 1      ,   2 union all 
select 2      ,   4 union all
select 3      ,   6 union all
select 4      ,   3 union all
select 5      ,   1 union all
select 6      ,   1 
insert into B
select 1     ,    '办公室'  union all
select 2     ,    '办公室'  union all
select 3     ,    '客服'    union all
select 4     ,    '客服'    union all
select 5     ,    '开发'    union all
select 6     ,    '开发' 
select 
B.department as '部门',
count(A.id)  as '部门员工数',
min(A.days)  as '该部门员工参加培训的最少天数'
from A,B 
where  
A.id=B.id 
group by B.department