从具有相同列的两个表中进行联接

时间:2019-05-15 12:36:26

标签: sql join oracle-sqldeveloper

创建两个表时可以找到我的解决方案,但是我希望找到一种无需创建两个表即可运行查询的方法。我遇到的问题是,这些表正在两个单独的表上联接,其中一个是历史表。我想在一个查询中显示一个ID与另一个ID之间的更改。

例如-ID 123以前的IDP为3,但现在为5。我希望查询提取该ID,IDP为5和Hist_IDP为3。

create table XYZ_table as
select a.ID,  a. IDP, a.CODE,  b.datecreated, c.file_id,  
a.modified_date, b.ID_YR, b.ID_CD, b.TYPE_CD, b.ID_DESC
from ID_TABLE a, DESC_TABLE b, TRANSACTION_TABLE c
where a.IDP = b.IDP
and a.ID = c.ID
and b.ID_yr >= 2000;

create table hist_table as 
select * from (
select a.*, HSTDATECREATED, b.IDP HIST_IDP, C.ID_yr hist_id_yr, c.TYPE_CD 
HIST_TYPE_CD, ROW_NUMBER() OVER (PARTITION BY a.ID order by 
HSTDATECREATED DESC) rank_1
from XYZ_table a, hist_table b, desc_table c
where a.id = b.id 
and b.IDP= c.IDP
and C.ID_yr <> a.ID_YR
and c.TYPE_CD != 'U')
where rank_1 = 1;


 ID IDP CODE    DATECREATED FILE_ID MODIFIED_DATE   ID_YR   ID_CD   TYPE_CD ID_DESC HSTDATECREATED  HIST_IDP    HIST_ID_YR  HIST_TYPE_CD    RANK_1
123DD   5   3   12/04/2010  xxx3395 04/26/2019  1980    UNDEFINED   A       04/26/2019  3   2010    T   1
423AA   4   3   12/04/2010  xxx5380 04/26/2019  1980    UNDEFINED   A       04/26/2019  1   2010    G   1
888CC   6   3   12/04/2010  xxx5829 04/26/2019  1980    UNDEFINED   A       04/26/2019  9   2010    G   1

0 个答案:

没有答案