Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3968230
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:49:02+00:00 2026-05-20T03:49:02+00:00

I have as a result from a dynamic query a table with only one

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:49:02+00:00Added an answer on May 20, 2026 at 3:49 am

    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 f values and the second column storing the other values) 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:

    SET @template = 'SELECT SUM(1) AS N,{f}, {other} FROM '+ @table_name
    

    then I replace in a loop the fields I need so,

    SET @template = REPLACE(@template, '{f}' , @f) 
    SET @template = REPLACE(@template, '{other}', @other)
    

    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 by to “squish” the rows down.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a result from an SQL query that I would like to sort
I have a Linq to Entities query like this one: var results = from
I currently have a SQL query that returns results based on a dynamic number
I have a stored procedure that contains dynamic sql to return a result set.
I have a script that appends some rows to a table. One of the
I am running oracle and have a query which pulls some results from the
I have a JSON result that contains numerous records. I'd like to show the
I have a script to extract certain data from a much bigger table, with
I have a sp which builds a dynamic sql query based on my input
I have a web application that builds a dynamic PDF with FPDF and allows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.