I have two tables whose content is as below.
Table 1:
ID1 ID2 ID3 ID4 NAME DESCR STATUS date
1 -12134 17773 8001300701101 name1 descr1 INACTIVE 20121203
2 -12136 17773 8001300701101 name1 descr1 INACTIVE 20121202
3 -12138 17785 9100000161822 name3 descr3 INACTIVE 20121201
4 -12140 17785 9100000161822 name3 descr3 ACTIVE 20121130
5 -12142 17787 8000500039106 name4 descr4 ACTIVE 20121129
Table2:
ID1 ID2 ID3 ID4 NAME DESCR
0 17781 17773 8001300701101 name1 descr1
0 17783 17783 8001300060109 name2 descr2
0 17785 17785 9100000161822 name3 descr3
0 17787 17787 8000500039106 name4 descr4
0 17789 17789 0000080052364 name5 descr5
I am trying to get below result.
ID3 ID4 NAME DESCR STATUS date
17773 8001300701101 name1 descr1 INACTIVE 20121202
17783 8001300060109 name2 descr2 NULL NULL
17785 9100000161822 name3 descr3 ACTIVE 20121201
17787 8000500039106 name4 descr4 ACTIVE 20121129
17789 0000080052364 name5 descr5 NULL NULL
As per the above i/p and o/p, the two tables should be joined based on columns id3, id4, name and desc. if an active record exists, it should return the active record. but if inactive record only exists, then oldest inactive record should be joined.
I tried different queries which are no longer near to the answer i wanted. The four columns joined are all non primary fields but not nulls. There can be one to many or many to many relationship between the two tables.
I am working on Apache phoenix and if the solution is in Hadoop Mapreduce or in Apache Spark also OK.
A sample query i have written is as follows.
Select table2.*, table1.status, table1.date
From table1 Right outer join table2 on table1.id3 = table2.id3
and table1.id4 = table2.id4
and table1.name = table2.name
and table1.descr = table2.descr
Order by (status) and order by (date)
Can any one help me please?