I have table as follows (let’s call it old data
).
UNIQUE_PJT | PROJECT | JOB_TITLE | SALARY_A | SALARY_B |
---|---|---|---|---|
IT_9810_PR | IT_9810 | Programmer | 10000 | 6000 |
IT_9810_AC | IT_9810 | Accountant | 9000 | 4500 |
IT_9810_SPR | IT_9810 | Programmer Sr | 15000 | 8000 |
IT_9810_SR | IT_9810 | Sales Representative | 12000 | 9000 |
- This data extends to thousand of rows with each
Project
having upto 100 rows. UNIQUE_PJT
is unique and can be used to compare data in the 2 tables.
Now I have newer salary data values in ditto same structure. Newer data facts:
- Some
Project
have been added, some have been removed - Some
Job Title
have been added, some have been removed
new data
table:
UNIQUE_PJT | PROJECT | JOB_TITLE | SALARY_A | SALARY_B |
---|---|---|---|---|
IT_9810_AC | IT_9810 | Accountant | 5000 | 8500 |
IT_9810_SPR | IT_9810 | Programmer Sr | 25000 | 12000 |
IT_9810_SR | IT_9810 | Sales Representative | 20000 | 15000 |
I want to show the changes in new data
based on old data
. Display pages are project and salary class specific. So, desired output is for Project IT_9810
and salary class SALARY_A
:-
PROJECT | JOB_TITLE | SALARY_A |
---|---|---|
IT_9810 | Programmer | 10000 (·) |
IT_9810 | Accountant | 5000 (-4000) |
IT_9810 | Programmer Sr | 25000 (+10000) |
IT_9810 | Sales Representative | 20000 (+8000) |
What should be the best approach for this.
I tried keeping the old data as table_old
and newer data as table_new
. Now on project display page, I selected all values corresponding to a Project from table_new
and for each result row of –UNIQUE_PJT
, queried table_old
. But this is very slow since it queries DB again and again.
2
Answers
You should use a single query that joins the old and new data tables based on the UNIQUE_PJT field.
This query produces complete comparing. You may remove some columns if you do not need in them. Also you may add WHERE which compares old and new values and and removes those rows which have no difference in the data.