I need to create a table (possibly a view) that relates two tables, one table of configs with a results table. The problem is that it’s necessary transform a simple row in the results table in multiple rows of the configs table.
I will give you an example of what I need:
Configs Table: primary key = (id_file,cpattr)
id_file cpattr type
------------------------------------
f01 dim01 merchant_id
f01 dim02 card_number
f01 dim03 trans_code
f02 dim01 card_number
f02 dim02 amount
Results Table
id_file dim01 dim02 dim03
-------------------------------------------------------
f01 01 88 015
f02 99 0.78 null
And I want to get a table(View) with the following output:
id_file type data
--------------------------------------------------
f01 merchant_id 01
f01 card_number 88
f01 trans_code 015
f02 card_numer 99
f02 amount 0.78
I’m not seeing a way of do that without using dynamic sql, but I’m trying to avoid it. Hope you can help me.
Thanks in advance.
Since it appears you only have the three dim columns in your results table, the easiest thing would be a case statement:
Edited to add:
Since there are an indeterminate number of dim columns, a slightly better approach might be:
You still have to identify all the dim columns affected, but you only have to do it once, not twice. This will also throw an error if any of the dim columns are removed or renamed, although it won’t populate from new dim columns. Not sure of any way to do that.