需要帮助计算使用SQL的教师薪水

时间:2014-11-13 17:04:21

标签: sql oracle oracle10g oracle-sqldeveloper

您好我正在尝试计算教师薪水,这是我的关系中的派生属性。下面是我的创建,插入和查询。我的问题是假设所有教师已经工作了3年。所有教师(高中,小学,中学)的基本工资都是2000美元。查询生成三个表,我只希望最后一个表具有正确的结果。我该如何解决?

谢谢。

查询结果(我只想要最后一张表):

STAFF_ID     SALARY
---------- ----------
  1006      24000
  1005      24000
  1003      24000
  1009      24000
  1007      24000
  1005      24000
  1004      24000
  1000      24000
  1003      24000
  1002      24000
  1009      24000

 STAFF_ID     SALARY
---------- ----------
  1001      24000
  1010      24000
  1007      24000
  1008      24000
  1002      24000
  1000      24000
  1004      24000
  1001      24000
  1006      24000
  1010      24000
  1008      24000

 STAFF_ID     SALARY
---------- ----------
  1009      30000
  1005      30000
  1003      30000
  1001      36000
  1007      36000
  1004      48000
  1010      48000
  1006      48000
  1008      48000
  1002      48000
  1000      48000

查询:

CREATE TABLE STAFF
(
     Staff_ID integer NOT NULL,
     Phone_num varchar(15),      
     Job_type varchar(20) NOT NULL,
     PRIMARY KEY (Staff_ID)
);

CREATE TABLE STAFF_LEVEL
(
    Staff_ID integer NOT NULL,
    Position_level  varchar(20),
    FOREIGN KEY (Staff_ID) REFERENCES STAFF(Staff_ID)
    ON DELETE CASCADE
);

insert into STAFF values(1000,'469-574-5637','Teacher');
insert into STAFF values(1001,'214-893-3744','Teacher');
insert into STAFF values(1002,'459-645-3433','Teacher');
insert into STAFF values(1003,'214-452-3432','Teacher');
insert into STAFF values(1004,'469-423-2344','Teacher');
insert into STAFF values(1005,'489-551-3004','Teacher');
insert into STAFF values(1006,'214-233-2872','Teacher');
insert into STAFF values(1007,'234-584-3231','Teacher');
insert into STAFF values(1008,'233-455-2933','Teacher');
insert into STAFF values(1009,'354-133-4911','Teacher');
insert into STAFF values(1010,'703-267-4191','Teacher');

insert into STAFF_LEVEL values(1000, 'High School');
insert into STAFF_LEVEL values(1001,'Middle School');
insert into STAFF_LEVEL values(1002, 'High School');
insert into STAFF_LEVEL values(1003, 'Elementary School');
insert into STAFF_LEVEL values(1004,'High School');
insert into STAFF_LEVEL values(1005,'Elementary School');
insert into STAFF_LEVEL values(1006,'High School');
insert into STAFF_LEVEL values(1007, 'Middle School');
insert into STAFF_LEVEL values(1008, 'High School');
insert into STAFF_LEVEL values(1009,'Elementary School');
insert into STAFF_LEVEL values(1010, 'High School');


-- Teachers have BaseSalary of ($2000). Assumes all teachers have been working for 3 years.-----
--- Elementary School additional        Salary = ($2000) + Level Bonus
select DISTINCT c.staff_id, (2000 + NVL(sub.bonus,0)) * 12 AS Salary
from STAFF_LEVEL, STAFF c LEFT JOIN (select staff_id, 500 AS bonus
               from STAFF_LEVEL
            where Position_level = 'Elementary School'
            group by staff_id
            ) sub ON c.STAFF_ID = sub.STAFF_ID
where job_type = 'Teacher' and Position_level = 'Elementary School'
UNION ALL
--- Middle School additional        Salary = ($2000) + Level Bonus
select DISTINCT c.staff_id, (2000 + NVL(sub.bonus,0)) * 12 AS Salary
from STAFF_LEVEL, STAFF c LEFT JOIN (select staff_id, 1000 AS bonus
            from STAFF_LEVEL
            where Position_level = 'Middle School'
            group by staff_id
            ) sub ON c.STAFF_ID = sub.STAFF_ID
where job_type = 'Teacher' and Position_level = 'Middle School'
UNION ALL
-- High School additional         Salary = ($2000) + Level Bonus
select DISTINCT c.staff_id, (2000 + NVL(sub.bonus,0)) * 12 AS Salary
from STAFF_LEVEL, STAFF c LEFT JOIN (select staff_id, 2000 AS bonus
            from STAFF_LEVEL
            where Position_level = 'High School'
            group by staff_id
            ) sub ON c.STAFF_ID = sub.STAFF_ID
where job_type = 'Teacher' and Position_level = 'High School'
order by Salary;

1 个答案:

答案 0 :(得分:1)

select c.staff_id, (2000 + NVL(sub.bonus,0)) * 12 AS Salary
from STAFF c INNER JOIN (select staff_id, case Position_level 
when 'Elementary School' then 500 
when 'Middle School' then 1000 
when 'High School' then 2000  end AS bonus
from STAFF_LEVEL  ) sub ON c.STAFF_ID = sub.STAFF_ID
where job_type = 'Teacher'
order by salary

希望你正在寻找这个。我建议你阅读很多。