Creating a salary report. Having trouble calculating previous salary. [message #633918] |
Fri, 27 February 2015 16:54 |
rappaj
Messages: 147 Registered: June 2010 Location: NYC
|
Senior Member |
|
|
Hi,
It's been a long time since I've posted here, but I am glad that you're still around. I could use some help. I've simplified what I have to do. Basicly, I need to create a salary report but I have to get the previous salary, and compare to the current to produce the increase. I hope this is all that is needed for testing and help. Thank you in advance. Joe
create table test_me
(name varchar2(5),
assignment_id varchar2(5),
change_date varchar2(4),
salary number);
insert into test_me values ('JOE', '16350', '2010', 103585);
insert into test_me values ('JOE', '16350', '2012', 115000);
insert into test_me values ('JOE', '16350', '2014', 135000);
commit;
select * from test_me;
NAME ASSIGNMENT_ID CHANGE_DATE SALARY
==== ============ ========== =========
JOE 16350 2010 103585
JOE 16350 2012 115000
JOE 16350 2014 135000
Need help with creating SQL that will make Result should look like this:
(salary-previous)
NAME salary PREVIOUS_salary INCREASE CHANGE_DATE
========= ========== =============== ================ ===========
JOE 135000 115000 20000 2014
JOE 115000 103585 11415 2012
JOE 103585 89000 14585 2010
|
|
|
|
|
|
|
Re: Creating a salary report. Having trouble calculating previous salary. [message #633924 is a reply to message #633923] |
Fri, 27 February 2015 18:04 |
rappaj
Messages: 147 Registered: June 2010 Location: NYC
|
Senior Member |
|
|
I sure will. I am still working on my SQL and have just 1 issue left before I post it. 99% done!
The problem I am having now is that I only want to display where the 'Diff' > 2 in this sample. Would you know how to do that?
I cannot do this --> and c1 - LAG(c1) OVER (ORDER BY id) > 2
--sample from link you sent
SELECT c1 "Curr",
LAG(c1) OVER (ORDER BY id) "Prev",
c1 - LAG(c1) OVER (ORDER BY id) "Diff"
FROM t1;
Curr Prev Diff
---------- ---------- ----------
40
45 40 5
35 45 -10
37 35 2 <---- I don't want this record because I only want the ones > 2
|
|
|
|
Re: Creating a salary report. Having trouble calculating previous salary. [message #633926 is a reply to message #633925] |
Fri, 27 February 2015 18:41 |
rappaj
Messages: 147 Registered: June 2010 Location: NYC
|
Senior Member |
|
|
Hi, This is my working SQL. However, if I also include and Diff > 8999
I get this error:
ORA-00904: "DIFF": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 117 Column: 7
--working sql
select first_name, salary, previous_salary, Diff from
(
SELECT
first_name, pro.proposed_salary_n Salary,
LAG(pro.proposed_salary_n) OVER (ORDER BY pro.proposed_salary_n) Previous_salary,
pro.proposed_salary_n - LAG(pro.proposed_salary_n) OVER (ORDER BY pro.proposed_salary_n) Diff,
TO_CHAR(pro.change_date, 'RRRR') change_Date
from PER_PAY_PROPOSALS PRO ,
apps.per_all_Assignments_f paaf,
apps.per_all_people_f papf
where pro.assignment_id in (7586) --(16350,7586,24270)
and pro.assignment_id = paaf.assignment_id
and papf.person_id = paaf.person_id
and TO_CHAR(pro.change_date, 'RRRR') > '2009'
--and Diff > 8999 (This is line 117 column 7 in error)
and sysdate between paaf.effective_start_date and paaf.effective_end_date
and sysdate between papf.effective_start_date and papf.effective_end_date
);
FIRST_NAME SALARY PREVIOUS_SALARY DIFF
========= ======= =============== ====
GEORGIANA 85878
GEORGIANA 94423 85878 8545
GEORGIANA 109000 94423 14577
GEORGIANA 132500 109000 23500
Did I do it incorrectly? I've tried so many different ways to get that where clause working, but no luck. I don't see how this piece of SQL could work though. and Diff > 8999
...still trying.
Thanks
[Updated on: Fri, 27 February 2015 21:22] Report message to a moderator
|
|
|
Re: Creating a salary report. Having trouble calculating previous salary. [message #633929 is a reply to message #633926] |
Fri, 27 February 2015 22:30 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
--and Diff > 8999 (This is line 117 column 7 in error)
Add it in the outer query because diff is not related to the inner query.
For example
SELECT NAME, SALARY, PREVIOUS_SALARY, nvl(salary-previous_salary,0) increase,CHANGE_DATE FROM (
select 'JOE' name ,sal salary , LAG(sal) OVER (ORDER BY empno) PREVious_salary,hiredate change_date from emp);
[Updated on: Fri, 27 February 2015 22:52] Report message to a moderator
|
|
|
Re: Creating a salary report. Having trouble calculating previous salary. [message #633936 is a reply to message #633929] |
Sat, 28 February 2015 07:26 |
rappaj
Messages: 147 Registered: June 2010 Location: NYC
|
Senior Member |
|
|
Thank you sss111ind for letting me know about adding it to the outer query. I guess that was what BlackSwan was trying to tell me to do too. pAdding that to the outer query worked. I also modified my query like you had suggested above. It is much cleaner that way too !!
I will post my results when I validate the data and all is good.
Thanks to you both, I appreciate the help.
|
|
|
Re: Creating a salary report. Having trouble calculating previous salary. [message #633965 is a reply to message #633936] |
Sun, 01 March 2015 20:16 |
rappaj
Messages: 147 Registered: June 2010 Location: NYC
|
Senior Member |
|
|
Hi all,
Thanks for all your help with this tricky SQL. It worked perfectly for 1 employee, but I had over 15000 in the report to run.
So I had to use a PARTITION BY in he SQL as well to break it by person_id. If I did not use PARTITION BY, it would
get the previous salary from the previous employee all the way down the report, which would be incorrect.
There might have been another way, but I figured out how to get it correct using that.
Here is the final code, and short sample report...
Thanks,
Joe
--increases greater than $8000
SELECT person_id, LAST_NAME ,
SALARY,
PREVIOUS_SALARY,
nvl(salary-previous_salary,0) increase,
CHANGE_DATE FROM (
select papf.last_name , papf.first_name, papf.person_id,
pro.proposed_salary_n salary ,
LAG(pro.proposed_salary_n)
OVER (PARTITION BY PAPF.PERSON_ID ORDER BY pro.proposed_salary_N) AS PREVious_salary,
pro.change_date change_date
from PER_PAY_PROPOSALS PRO ,
apps.per_all_Assignments_f paaf,
apps.per_all_people_f papf
where pro.assignment_id = paaf.assignment_id
and papf.person_id = paaf.person_id
AND pro.change_date > '01-JAN-2009'
and sysdate between paaf.effective_start_date and paaf.effective_end_date
and sysdate between papf.effective_start_date and papf.effective_end_date
)
WHERE nvl(salary-previous_salary,0) >= 8000
order by 2,1,6 DESC;
PERSON_ID LAST_NAME SALARY PREVIOUS_SALARY INCREASE CHANGE_DATE
======== ============ ======== ============= ======== ===========
29033 AARON 26843 17465 9378 26-May-12
17702 ABDOOL 72558 60552 12006 20-Feb-10
01649 ABDULLAH 54580.32 45632 8948.32 22-Nov-14
16947 ABDUS-SABUUR 99007 78946 20061 30-Aug-14
04285 ABREU 54580.32 45632 8948.32 26-May-12
33203 ACEVEDO 46378 36126 10252 12-Apr-14
12435 ADAMES 100000 69608 30392 22-Nov-14
16744 ADAMES 82337 67337 15000 24-Jul-10
00930 ADAMS 91433.52 72558 18875.52 1-Mar-14
|
|
|
Re: Creating a salary report. Having trouble calculating previous salary. [message #633999 is a reply to message #633965] |
Mon, 02 March 2015 12:34 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
rappaj wrote on Sun, 01 March 2015 21:16
AND pro.change_date > '01-JAN-2009'
PERSON_ID LAST_NAME SALARY PREVIOUS_SALARY INCREASE CHANGE_DATE
======== ============ ======== ============= ======== ===========
29033 AARON 26843 17465 9378 26-May-12
How is that possible when you show change date as a character representation of a 4 digit year?
A string representation of any year in the 21st century will always be greater than a string that starts with a zero.
[Updated on: Mon, 02 March 2015 12:37] Report message to a moderator
|
|
|
|