I have been provided with some data in an SQL table, which needs to be transformed according to some rules. However, this transformation requires dynamically addressing column names, i.e., the names of columns in which values should be stored are themselves stored as values in other columns, etc. Also, depending on if values are present in a range of different columns, multiple output rows should be produced for one single input row, so this is a kind of transposing I guess.
Let me illustrate it with an example:
id | targetColumnName | col1 | col2 | col3 | col4
-----------------------------------------------------
1 foo 5
2 foo 7 42
3 bar 3 6
4 foo 5
5 bar
6 bar 2 12
This should produce the following:
id | foo | bar
--------------
1 5
2 7
2 42
3 3
3 6
4 5
6 2
6 12
In other words, for each present value in any of the colX columns, produce an output row with that value in the column designated by the targetColumnName column.
This leads me to believe that using SQL at all is not a good idea, at least not by creating a simple query. It's probably possible to create a compound statement / stored procedure, but still seems like the wrong tool for the job to me.
I have the data also in csv format, and I have a feeling that a powerful script language with text manipulation capabilities would suit the job better, but I have only scratched the surface of Python, I don't know any Perl and only a little awk, etc. I know I could pull it off in Java, but I still have a feeling this can be done with pretty much a magic one-liner in Python. :)
So the questions are essentially 1) is it reasonable to do this with SQL(ite) and 2) what other tool would be better?