I need help with a Join NOT WORKING in PL/SQL Developer under Oracle SQL. I have a table VALUES which have multiple columns. Its structure is:
CYCLE|PERIOD|FEE|CITY|SERVICE_NAME|CLIENT_TYPE|VALUE
what I want to do is to add another column new to VALUE, and name it as PREVIOUS_VALUE. It will contain the field VALUE for the row which have the same values in CYCLE, FEE, CITY, SERVICE_NAME and CLIENT_TYPE, the only difference is that it belong to a PERIOD' = PERIOD - 1. In other words, I want to add the value of the previuos period next to the VALUE in the table.
My strategy was the following: 1) Create a view VV_VIEW which contains the same fields as the table VALUES, created as following:
CREATE OR REPLACE VIEW VV_VIEW as SELECT CYCLE, PERIOD,
PREVIOUS_PERIOD(PERIOD) AS PREV_PERIOD, FEE, CITY, SERVICE_NAME,
CLIENT_TYPE, VALUE FROM VALUES.
2) Do a FULL OUTER JOIN on VV_VIEW AND VALUES selectin all the columns from VV_VIEW and ADDING IT the column VALUE from the table VALUES with the condition that VALUES.PERIOD = VV_VIEW.PREVIOUS_PERIOD and also VALUES.CYCLE = VV_VIEW.CYCLE, VALUES.FEE = VV_VIEW.FEE, VALUES.CITY = VV_VIEW.CITY, etc. The exact query looks like this:
CREATE OR REPLACE VIEW FINAL_VIEW AS SELECT VV_VIEW.CYCLE,
VV_VIEW.PERIOD, VV_VIEW.PREV_PERIOD, VV_VIEW.FEE, VV_VIEW.CITY,
VV_VIEW.SERVICE_NAME, VV_VIEW.CLIENT_TYPE, VALUES.VALUE AS PREV_VALUE
FROM VV_VIEW FULL OUTER JOIN VALUES
ON VV_VIEW.PREV_PERIOD = VALUES.PERIOD AND VV_VIEW.FEE = VALUES.FEE AND
VV_VIEW.CITY = VALUES.CITY AND VV_VIEW.SERVICE_NAME =
VALUES.SERVICE_NAME AND
VV_VIEW.CLIENT_TYPE = VALUES.CLIENT_TYPE;
So that the VALUE from the previous period will appear next to the actual VALUE in the new view.
The problem is, FULL OUTER JOIN does not seem to work. When I use RIGHT OUTER JOIN I get the values in the PREV_VALUE COLUMN, but the other columns are NULL. If I use LEFT OUTER JOIN I get all the columns with their respective valur, except for PREV_VALUE, which remains NULL. With the FULL OUTER JOIN I get results similars to those of using LEFT JOIN.
Could you please tell me what am I doing wrong?
What I want, in simple terms is something like this:
** TABLE VALUES = ORIGINAL TABLE
CYCLE|PERIOD|FEE|CITY|SERVICE_NAME|CLIENT_TYPE|VALUE
03 |201404|0.5|CTTA|TELEPHONE_SM| PREMIUM | 3000
03 |201405|0.7|CMFE|TELEPHONE_MM| PREMIUM | 2000
04 |201312|0.2|CTDA|TELEPHONE_SM| STANDARD | 500
.....
03 |201403|0.5|CTTA|TELEPHONE_SM| PREMIUM | 9000
03 |201404|0.7|CMFE|TELEPHONE_MM| PREMIUM | 8000
.....
03 |201402|0.5|CTTA|TELEPHONE_SM| PREMIUM | 1000
03 |201403|0.7|CMFE|TELEPHONE_MM| PREMIUM | 2000
.....
and get this new view:
***FINAL_VIEW
CYCLE|PERIOD|PREV_PERIOD|FEE|CITY|SERVICE_NAME|CLIENT_TYPE|VALUE | PREV_VALUE
03 |201404 | 201403 |0.5|CTTA|TELEPHONE_SM| PREMIUM | 3000 | 9000
03 |201405 | 201404 |0.7|CMFE|TELEPHONE_MM| PREMIUM | 2000 | 8000
04 |201312 | 201403 |0.2|CTDA|TELEPHONE_SM| STANDARD | 500 | ....
.....
03 |201403 | 201402 |0.5|CTTA|TELEPHONE_SM| PREMIUM | 9000 | 1000
03 |201404 | 201403 |0.7|CMFE|TELEPHONE_MM| PREMIUM | 8000 | 2000
.....
03 |201402 | 201401 |0.5|CTTA|TELEPHONE_SM| PREMIUM | 1000 | .....
..............
I hope you get hat I'm trying to do. If you have an alternative approach to accomplish this, I would really like to hear from it. Thanks in advance.
EDIT: Fixed a typo in the query.