|
|
发表于 2007/8/24 13:28:24
|
显示全部楼层
表A: DEPT
字段:DEPT_NUM,DEPT_NAME
表B:EMPLOYEE
字段:DEPT_NUM,EMP_NUM,EMP_NAME,EMP_AGE,EMP_NATION,EMP_POSITION_NEW
表C OSITION
字段:EMP_NUM,EMP_POSITION_OLD,EMP_POSITION_NEW
创建表以及往里面插入测试数据:
Create Table DEPT(DEPT_NUM Number(10),DEPT_NAME Varchar2(20))
Create Table EMPLOYEE(DEPT_NUM Number(10),EMP_NUM Number(10),EMP_NAME VARCHAR2(20),EMP_POSITION_NEW Varchar2(20),EMP_AGE Number(10))
Create Table POSITION(EMP_NUM Varchar(10),EMP_POSITION_OLD Varchar2(20),EMP_POSITION_NEW Varchar2(20))
Insert Into DEPT Values(100,'计算机系');
Insert Into DEPT Values(120,'自动化系');
Insert Into DEPT Values(130,'英语系');
Insert Into DEPT Values(140,'会计系');
Insert Into EMPLOYEE Values(100,001,'张三','主任',23);
Insert Into EMPLOYEE Values(120,002,'李四','处长',34);
Insert Into EMPLOYEE Values(130,003,'马五','局长',35);
Insert Into EMPLOYEE Values(140,004,'王六','院长',55);
Insert Into POSITION Values(001,'副主任','主任');
Insert Into POSITION Values(001,'主任助理','副主任');
Insert Into POSITION Values(002,'副处长','处长');
Insert Into POSITION Values(003,'副局长','局长');
Insert Into POSITION Values(003,'主任','副局长');
Insert Into POSITION Values(003,'副主任','主任');
Insert Into POSITION Values(004,'副院长','院长');
Insert Into POSITION Values(004,'主任','副院长');
Insert Into POSITION Values(004,'副主任','主任');
Insert Into POSITION Values(004,'处长','副主任');
Insert Into POSITION Values(004,'副处长','处长');
Insert Into POSITION Values(005,'主任助理','副处长');
第一个问题
SELECT DEPT_NAME,EMP_NAME,d.NUM
FROM DEPT a,EMPLOYEE b,
(SELECT c.EMP_NUM,count(c.EMP_POSITION_OLD) NUM
FROM POSITION c
GROUP BY c.EMP_NUM) d
WHERE a.DEPT_NUM=b.DEPT_NUM
AND b.EMP_NUM=d.EMP_NUM
AND d.NUM>=2
第二个问题
SELECT a.NUM 从20到29,b.NUM 从30到39 ,c.NUM 从40到49,d.NUM 大于50
FROM
(SELECT COUNT(EMP_NUM) NUM
FROM EMPLOYEE
WHERE EMP_AGE>=20
AND EMP_AGE<=29
) a,
(SELECT COUNT(EMP_NUM) NUM
FROM EMPLOYEE
WHERE EMP_AGE>=30
AND EMP_AGE<=39
) b,
(SELECT COUNT(EMP_NUM) NUM
FROM EMPLOYEE
WHERE EMP_AGE>=40
AND EMP_AGE<=49
) c,
(SELECT COUNT(EMP_NUM) NUM
FROM EMPLOYEE
WHERE EMP_AGE>=50
) d |
|