I have as a result from a dynamic query a table with only one row, it has 1 column + d + n columns so, the problem here is that the number of ‘d’ and ‘n’ is variable so I could have a row with 5,6,7,..d values, and 10,11….n values or more…
like
Input:
x f1 f2 f3 ... fd Other1 Other2 Other3 ... Othern
10 1.0000 139.0000 60.0000 ... 59.0000 846.0000 30.1000 0.3980 ... 0.398
If I need to do some calculus with, lets say, n,f1,Other1 for the first column; n,f1,f2,Other1,Other2 for column 2, n,f1,f3,Other1,Other3 for third column… of another table like:
Column_1 Column_2 Column_3 ..Column_d
x*(f1*f1)/(Other1*Other1) x*(f1*f2)/(Other1*Other2) x*(f1*f3)/(Other1*Other3)..x*(f1*fn)/(Other1*Othern)
x*(f2*f1)/(Other2*Other1) x*(f2*f2)/(Other2*Other2) x*(f2*f3)/(Other2*Other3)..x*(f2*fn)/(Other2*Othern) ...
...
x*(fd*f1)/(Otherd*Other1) x*(fd*f2)/(Otherd*Other2) x*(fd*f3)/(Otherd*Other3)..x*(fd*fn)/(Otherd*Othern)
I was thinking to first save the columns that I need in a nested loop, and updating it until I reach the end of the table. but As I need to do that d times I’m getting a little confused, so my questions are:
- Could I use a cursor to get the output?
- Could I select all vars first to do that?
- Using a pivot should do the trick, How?
- Do not know, and the main issue is that the input table has d dynamic columns
I am trying to do a Stored procedure but have no luck, dynamically constructing SQL query in code before executing.
Thanks in advance.
Hope the question is more readeable. thank you
PS.
The x is another input, so it has nothing to do with the ‘n’ elements in columns Other1…Othern
————–EDIT—————–
To generate the input table with one row:
I use a dynamic query to select various fields, As they are dynamic I use a string which will be replaced later so the general code is:
SET @template = 'SELECT SUM(1) AS x,{f}, {other} FROM '+ @table_name
--then in some loops I calculate sums, powers, etc...
--so after I replace the strings with cosen queries I replace them like
SET @template = REPLACE(@template, '{f}' , @dynamicStringForf )
SET @template = REPLACE(@template, '{Other}', @dynamicStringForOther )
--Finally I get large query with the objetive I need
--something like:
'SELECT SUM(1) AS x, sum(a+b) as f1,pow(b,c) as f2....,sum(x+y) as Other1 ,pow(y+z) as Other 2... FROM '+ @table_name
the result is a one row with data like:
x f1 f2 f3 ... fd Other1 Other2 Other3 ... Othern
10 1.0000 139.0000 60.0000 ... 59.0000 846.0000 30.1000 0.3980 ... 0.398
Now I have created a new temp table dynamically
--@d could be any number, but at this stage I know it
Set @TempColumn = ''
Set @TempCol = ''
Set @Comma = ''
Set @ColumnNo = 1
Set @SQL = 'Create Table temp ('
WHILE @ColumnNo <= @d Begin
Set @TempColumn =@TempColumn + @Comma + ' Column_' + Cast(@ColumnNo as nvarchar)
Set @SQL =@SQL + @Comma + ' Column_' + Cast(@ColumnNo as nvarchar) + ' FLOAT'
Set @Comma = ','
Set @ColumnNo = @ColumnNo + 1
END
Set @SQL = @SQL + ' )'
EXEC (@SQL) --create temp table
--the result is a new table like:
Column_1 Column_2 Column_3 ... Column_d
Now I want to populate it, something like:
Column_1 Column_2 Column_3 ..Column_d
x*(f1*f1)/(Other1*Other1) x*(f1*f2)/(Other1*Other2) x*(f1*f3)/(Other1*Other3)..x*(f1*fn)/(Other1*Othern)
x*(f2*f1)/(Other2*Other1) x*(f2*f2)/(Other2*Other2) x*(f2*f3)/(Other2*Other3)..x*(f2*fn)/(Other2*Othern) ...
...
x*(fd*f1)/(Otherd*Other1) x*(fd*f2)/(Otherd*Other2) x*(fd*f3)/(Otherd*Other3)..x*(fd*fn)/(Otherd*Othern)
Any idea how to acomplish this, union, cursor, pivot, what could be the best
SQL is really good at working with sets of data — as you want to do, if that data is stored as rows.
I would think the best way to solve this problem is to transform the data into a table with two columns (one column storing the
fvalues and the second column storing theothervalues) with D rows.Then the solution is fairly simple (a join and a pivot statement).
Even better — re-write the prior query to give you the data in this format. (Do you have the prior query — I could show you how to do that.
Well to generate the row I described I have something like:
then I replace in a loop the fields I need so,
I don’t understand how this works… it looks like you are just selecting variables — are those variables column names? Please clarify — I’m sure there is a better way to build this query.
Could you explain me how to do that join and that pivot you describe?
If I use a pivot, how do I change the selected columns, to compute the terms as I described?
I will when we know what your data structure looks like for sure, I need something to test against.
f has d data and Other has n, if I put the values ina 2 column table how fo I handle the n > d, and a lot of nulls in the d column??
Often times when you have a lot of nulls you use
group byto “squish” the rows down.