简体   繁体   English

替换列值的字符串的最快方法SQL Server

[英]Fastest way to replace string for column values SQL Server

I have the following tables: 我有以下表格:

Table_1 表格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 表_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. 我想在table_2的公式中替换table_1的值。 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)

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]) 确保公式将字段名称括在方括号内,例如([par01]*[par02])
  • Create a dynamic query to pivot all those table_1 values as columns 创建一个动态查询以将所有这些table_1的值作为列进行透视
  • Combine the pivoted dynamic query with the formula values and it will be "natural" for the SQL to execute 将枢轴动态查询与公式值结合使用,SQL将自然执行

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. 请注意动态区域。

To retreive a value, you have to use SELECT So your query can be : 要获取值,必须使用SELECT因此查询可以是:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM