0

I have the following tables:

Table_1

timestamp                  parameter     value
---------------------------------------------------
2015-09-04 18:48:00.000    par01          1
2015-09-04 18:48:00.000    par02          2
2015-09-04 18:48:00.000    par03          3
2015-09-04 18:48:00.000    par04          4
2015-09-04 18:48:00.000    par05          5
2015-09-04 18:48:00.000    par06          6

Table_2

formulaID      formula                 
---------------------------------------------------
1              (par01*par02)
2              (par03*par04)
3              (par05*par06)

I want to replace the values of the table_1 in the formulas of the table_2. Finally, execute the formula. What's the fastest way?

I know how execute the formulas with the following code:

DECLARE @sql VARCHAR(50)
SET @sql = '(2*2)'

EXEC('select ' + @sql)
4
  • You need to be more specific. Otherwise you just want AVG() Commented Sep 4, 2015 at 18:20
  • @Amit what happen with the answer I saw before with the AVG ? Commented Sep 4, 2015 at 18:58
  • @SeanLange Why you delete your answer? Commented Sep 4, 2015 at 19:12
  • We can not use AVG() in this case. Because formula is: ( par01 + par01 + par03 )/ 3. @Josep Is it correct formula? Commented Sep 4, 2015 at 19:55

2 Answers 2

1

I already did this in the past, and it's a bit tricky:

  • Make sure the formulas encloses the field names within square brackets, like ([par01]*[par02])
  • Create a dynamic query to pivot all those table_1 values as columns
  • Combine the pivoted dynamic query with the formula values and it will be "natural" for the SQL to execute

A glimpse on that result would be like this.

;WITH   Table_1 AS
(
        SELECT  *
        FROM
        (       VALUES
                ('2015-09-04 18:48:00.000', 'par01', 1),
                ('2015-09-04 18:48:00.000', 'par02', 2),
                ('2015-09-04 18:48:00.000', 'par03', 3),
                ('2015-09-04 18:48:00.000', 'par04', 4),
                ('2015-09-04 18:48:00.000', 'par05', 5),
                ('2015-09-04 18:48:00.000', 'par06', 6)
        )       Table_1([timestamp], [parameter], [value])
)
SELECT  *
FROM
(
        SELECT  ([par01]*[par02]) as [formulaId=1], --
                ([par03]*[par04]) as [formulaId=2], -- This area must be dynamic
                ([par05]*[par06]) as [formulaId=3]  --
        FROM    Table_1 
        PIVOT   (       SUM(value) FOR
                        [parameter] in
                        (
                            [par01], [par02], [par03], -- This area must
                            [par04], [par05], [par06]  -- be dynamic too
                        )
                )       pvt
)       p
UNPIVOT
(
        Value FOR FormulaId IN 
        (
            [formulaId=1],  -- 
            [formulaId=2],  -- Another dynamic area
            [formulaId=3]   -- 
        )
)       upvt

Please, notice the dynamic areas.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to avoid the pivot? In the first row show the result of [formulaId=1] in the second [formulaId=2] and in the last row the result of [formulaId=3]
No you can't avoid it, as you'll the columns names to evaluate those formulas. But you can perform a UNPIVOT after it; I updated the answer to reflect this change
0

To retreive a value, you have to use SELECT So your query can be :

DECLARE @sql VARCHAR(500)
SET @sql = '((select value from t where parameter = ''par01'') +
             (select value from t where parameter = ''par02'') +
             (select value from t where parameter = ''par03'')) / 3'


EXEC('select ' +  @sql)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.