简体   繁体   English

PL / SQL Developer中的Oracle SQL上的两个表和多个条件之间的完全外部联接不起作用

[英]Full outer join not working between two tables and multiple conditions on Oracle SQL in PL/SQL Developer

I need help with a Join NOT WORKING in PL/SQL Developer under Oracle SQL. 我需要有关Oracle SQL下PL / SQL Developer中的Join NOT WORKING的帮助。 I have a table VALUES which have multiple columns. 我有一个包含多个列的表VALUES。 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. 我想要做的是向VALUE添加新的另一列,并将其命名为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. 它包含行的VALUE字段,在CYCLE,FEE,CITY,SERVICE_NAME和CLIENT_TYPE中具有相同的值,唯一的区别是它属于PERIOD'= PERIOD-1。换句话说,我想添加表中VALUE旁边的上一个周期的值。

My strategy was the following: 1) Create a view VV_VIEW which contains the same fields as the table VALUES, created as following: 我的策略如下:1)创建一个视图VV_VIEW,其中包含与表VALUES相同的字段,创建方式如下:

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: 2)在VV_VIEW和VALUES上进行FULL OUTER JOIN,从VV_VIEW中选择所有列,并从VALUES表中添加VALUE列,条件是VALUES.PERIOD = VV_VIEW.PREVIOUS_PERIOD以及VALUES.CYCLE = VV_VIEW.CYCLE,VALUES。 FEE = VV_VIEW.FEE,VALUES.CITY = VV_VIEW.CITY等。确切的查询如下所示:

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. 这样,上一期间的VALUE将显示在新视图中的实际VALUE旁边。

The problem is, FULL OUTER JOIN does not seem to work. 问题是,FULL OUTER JOIN似乎不起作用。 When I use RIGHT OUTER JOIN I get the values in the PREV_VALUE COLUMN, but the other columns are NULL. 当我使用RIGHT OUTER JOIN时,我在PREV_VALUE COLUMN中获得了值,但其他列为NULL。 If I use LEFT OUTER JOIN I get all the columns with their respective valur, except for PREV_VALUE, which remains NULL. 如果我使用LEFT OUTER JOIN,则除PREV_VALUE之外,所有列均带有各自的评估者,但保留为NULL。 With the FULL OUTER JOIN I get results similars to those of using LEFT JOIN. 使用FULL OUTER JOIN可获得与使用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 *** 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. 编辑:修复了查询中的错字。

Try below, you should get your expected results, I have used "0" (third parameter in lag function ) when previous value is not present you can use null or any other value 尝试以下操作,您应该得到预期的结果,当不存在前一个值时,我可以使用“ 0”(滞后函数中的第三个参数),可以使用null或任何其他值

select 
cycle,
period,
fee,
city,
service_name,
client_type,
value,
lag(period,1,0) over (partition by cycle,fee,city,service_name,client_type order by period) as prev_period,
lag(value,1,0) over (partition by cycle,fee,city,service_name,client_type order by period) as prev_value
from table1
order by cycle,fee,city,service_name,client_type,period;

2nd Query to verify for previous 3 values 第二次查询以验证前三个值

select 
cycle,
period,
fee,
city,
service_name,
client_type,
value,
row_number() over (partition by cycle,fee,city,service_name,client_type order by period) as rn,
lag(value,1,0) over (partition by cycle,fee,city,service_name,client_type order by period) as prev_value1,
lag(value,2,0) over (partition by cycle,fee,city,service_name,client_type order by period) as prev_value2,
lag(value,3,0) over (partition by cycle,fee,city,service_name,client_type order by period) as prev_value3
from table1
order by cycle,fee,city,service_name,client_type,period;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM