I have a file like below .
Table1|Column1
Table2|Column1
Table5|Column1
Table3|Column2
Table2|Column2
Table4|Column3
Table2|Column3
Table2|Column4
Table5|Column4
Table2|Column5
From the below file i am trying to generate a Dynamic SQL JOIn if Tablenames in Column1 have same Attributes
select * from Table1 a inner join Table2 b on a.Column1=b.column1 inner join Table5 c on a.Column1=c.column1
and
select * from Table3 a inner join Table2 b on a.column2 = b.column2
..etc till end of file
What is the best way to do it , please advise.
Please note that Same column can appear in more than 2 tables (like upto 20 tables then join will be repeated 19 times)
This is not a complete answer. However, I think you should be able to solve the problem using this answer as a stepping stone.
We use GNU awk
for parsing. For better readability we use a script file parse.awk
instead of one long command.
# parse.awk
{ a[$2][$1] };
END {
for (col in a) {
printf "%s", col;
for (tab in a[col])
printf "|%s", tab;
print ""
}
}
When we call the script ...
awk -F\| -f parse.awk yourFile
... on your example the output is
Column1|Table5|Table1|Table2
Column2|Table2|Table3
Column3|Table2|Table4
Column4|Table5|Table2
Column5|Table2
From there you should be able to build your SQL commands. You could even adapt parse.awk
to generate the SQL commands directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.