create table employee_table (employee_id number,employee_name varchar2(100),employee_designation varchar2(100),joining_date date,record_updated_date date);
insert all
into employee_table values(24321,'john','consultant','01-Jan-18','01-Jan-18')
into employee_table values(24322,'andrew','senior business analyst','01-Jan-15','01-Jan-15')
into employee_table values(24322,'andrew','consultant','01-Jan-18','01-Jan-17')
into employee_table values(24322,'andrew','senior consultant','01-Jan-15','01-Jan-19')
into employee_table values(24323,'simons','senior business analyst','01-Jan-17','01-Jan-17')
into employee_table values(24323,'simons','consultant','01-Jan-17','01-Jan-19')
select * from dual;
create table salary_table(employee_id number,year number,salary number);
insert all
into salary_table values(24321,2018,30000)
into salary_table values(24321,2019,30000)
into salary_table values(24322,2015,25000)
into salary_table values(24322,2016,25000)
into salary_table values(24322,2017,30000)
into salary_table values(24322,2018,30000)
into salary_table values(24322,2019,35000)
into salary_table values(24323,2017,25000)
into salary_table values(24323,2018,25000)
into salary_table values(24323,2019,30000)
select * from dual;
我现在创建了两个表,我想编写查询以获取每个雇员到现在为止所提取的总薪水。 请帮助我解决问题
答案 0 :(得分:1)
感谢您的测试用例。
似乎您知道您要做的一切;我不确定为什么你还没有这样做。
SQL> select e.employee_name,
2 sum(s.salary) total_salary
3 from (select distinct e1.employee_id,
4 e1.employee_name
5 from employee_table e1
6 ) e join salary_table s on s.employee_id = e.employee_id
7 where s.year <= 2017
8 group by e.employee_name;
EMPLOYEE_NAME TOTAL_SALARY
-------------------- ------------
simons 25000
andrew 80000
SQL>
这就是您所说的-这就是代码的作用:
sum
函数GROUP BY
子句(第8行):未聚合的列必须包含在其中。
employee_table
每位员工包含几行(根据他们多年来的工作地点);这就是为什么我使用内联视图(第3-5行)来获取不同员工的数据