I have three tables table1, table2 and table 3. I am getting data to a file with sequence of the output as shown below. I want to append the data coming in that file to a table4 that ill create and persist using Java.
Scenario: I want all the data in this three tables into one cloumn where table2 element matches table3 element in C4 then instead of that matched element i want to replace that element with the corresponding data in C5. For example in below mentioned table we see 11 in table2 matches table3 at column C4. Then the output should have 11_1, 11_2 instead of 11.
Help needed
Table1: Table2: Table3:
C1 C3 C4 C5
----- ------ ------ -------
1 11 11 11_1
2 12 11 11_2
3 13 14 14_1
4 14 14 14_2
5 15 14 14_3
16 null null
Code I worked:
SELECT c1
FROM (SELECT c1
FROM table1
UNION ALL
SELECT c3
FROM table2
UNION ALL
SELECT c5
FROM table3) temp
This query fetching me everything 🙁
SELECT C1 FROM(SELECT DISTINCT C1 AS C1 FROM table1 UNION ALL SELECT DISTINCT C2 FROM table2)AS
temp LEFT JOIN table3 ON .temp.C1=table3.C4 CASE WHEN table3.C4=NULL THEN temp.C1 ELSE table3.C5
Output Required:
Table4 C1 ----- 1 2 3 4 5 11_1 11_2 12 13 14_1 14_2 14_3 15 16
Help is appreciated
This should work:
Result
See the demo