From the below file i am trying to generate a Dynamic SQL Join between tablenames in column 1 when they have same value for columnname in column 2 of above table.
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
i want to do this by traversing through the file row by row till i reach end of file.
Can you please advise What is the best way to do it ?
Note: If i can just get the table names sharing same column names then also i can create the dynamic SQL outside the Bash logic.
Any attempts / ideas / thoughts from your side? How would you "create the dynamic SQL outside the Bash logic"? Do solution proposals have to be shell or would text tools like e.g. awk be acceptable as well?
1) At max I can get 2 tables with common join columns . If there are 3 tables with same columns i will have to split it into 2 separate SQL statement with one join each.
2) I need to remove functional duplicates . that is joins between same tables , joins where just table names are reversed table1 join table2 == table2 join table1
Note:
1) I am open to using awk , or sed ..or anything that works.
2) outside Bash i meant , i can use hard coded concatenation to generate most part of the SQL statement apart from the join part.
Saying, "unfortunately our awk version did not match" makes it clear that the operating system and shell you're using is important information that we need to know to be able to supply you with suggestions that will work in your environment. (And, this information should be supplied in every thread started in this forum.) From what you have said above, I would assume that you're using bash , but we don't know which version. So, please tell us what operating system (including the release/version number) and which version of bash you're using so we can provide you with suggestions that will work in your environment.
awk -F\| '
{TMP[$2] = TMP[$2] FA[$2] $1
FA[$2] = FS
}
END {CH[1] = 97
for (t in TMP) {n = split (TMP[t], T)
printf "select * from %s %c", T[1], CH[1]
for (i=2; i<=n; i++) {CH = CH[i-1] + 1
printf " inner join %s %c on %c.%s=%c.%s", T, CH, CH[1], t, CH, t
}
printf RS
}
}
' file
select * from Table1 a inner join Table2 b on a.Column1=b.Column1 inner join Table5 c on a.Column1=c.Column1
select * from Table3 a inner join Table2 b on a.Column2=b.Column2
select * from Table4 a inner join Table2 b on a.Column3=b.Column3
select * from Table2 a inner join Table5 b on a.Column4=b.Column4
select * from Table2 a
EDIT: or mayhap with a simpler END section:
END {split ("a b c d e f g", CH, " ")
for (t in TMP) {n = split (TMP[t], T)
printf "select * from %s %c", T[1], CH[1]
for (i=2; i<=n; i++) printf " inner join %s %c on %c.%s=%c.%s", T, CH, CH[1], t, CH, t
printf RS
}