具有2个表和1个SQL Server的动态操作

时间:2011-02-28 22:17:48

标签: sql sql-server tsql

我有两张桌子:

表1

id someval someatt
-------------------
1      23   name1
2      56   name2
3      76   name3
4     456   name4
5      53   name5
6      67   name6
7      12   name7
8      43   name8
9      99   name9
10    567   name10

表2

    id someval someatt
    -------------------
    1     23.3   name1
    2     5.6    name2
    3     8.76   name3
    4     4.56   name4
    5      5.3   name5
    6      6.7   name6
    7      1.2   name7
    8      4.3   name8
    9      9.9   name9
    10    56.7   name10

我需要在新表中插入两个表的各个字段的一些操作,例如

iteration1
x = get value from table1 where id 1 
y = get value from table1 where id 2
a = get value from table2 where id 1
b = get value from table2 where id 2

iteration2
r =  get value from table1 where id 2
s =  get value from table1 where id 3
u = get value from table2 where id 2
v = get value from table2 where id 3


iterationn.
q =  get value from table1 where id n-1
p =  get value from table1 where id n
o = get value from table2 where id n-1
e = get value from table2 where id n

然后插入NEWTABLE

(a*b + x+y), (r*s + u*v) ...(q*p+o*e) 
我正在考虑(如果我必须这样做100次):

SET @counter  = 1;
SET @template = '';
SET @N = 100;
WHILE(@counter < @N)
  Select @x =  value from table 1 where id = @counter
  Select @y =  value from table 1 where id = @counter + 1
  Select @a =  value from table 2 where id = @counter
  Select @b =  value from table 2 where id = @counter + 1
  @template = @template + ' (@x*@y + @a*@b), '
end

所以在循环结束时我有一个完整的模板

然后EXEC(@template)

  • 你怎么能优化这个?
  • 如何做我描述的事情? (table1和table2是我表的小例子)
  • 有没有办法4选择 避免陈述?

输出表:

result1  result2 result3 .... result100
---------------------------------------
 float     float   float  ...    float   

其中float是为table1和table2

中的每个值计算(a*b + x+y)的结果

4 个答案:

答案 0 :(得分:1)

这适用于SQL 2005&amp;使用您列出的相同数据。

SELECT  t1x.id AS smaller_id,
        ( t1x.someval * t1y.someval ) + ( t2a.someval * t2b.someval ) AS result
FROM    dbo.table1 AS t1x
        INNER JOIN dbo.table1 AS t1y ON t1x.id + 1 = t1y.id
        INNER JOIN dbo.table2 AS t2a ON t2a.id = t1x.id
        INNER JOIN dbo.table2 AS t2b ON t2b.id = t1y.id

如果你想要它是水平的,你必须PIVOT结果。

SELECT  'Calculation Result' AS CalcRes,
        [result1], [result2], [result3], [result4], [result5], [result6], [result7], [result8], [result9]
FROM    (
            SELECT  'result' + cast(t1x.id AS varchar(4)) AS result_name, 
                    ( t1x.someval * t1y.someval ) + ( t2a.someval * t2b.someval ) AS result
            FROM    dbo.table1 AS t1x
                    INNER JOIN dbo.table1 AS t1y ON t1x.id + 1 = t1y.id
                    INNER JOIN dbo.table2 AS t2a ON t2a.id = t1x.id
                    INNER JOIN dbo.table2 AS t2b ON t2b.id = t1y.id
        ) AS Results
PIVOT (
    min( result ) --there needs to be an aggregate function here
    FOR result_name IN ([result1], [result2], [result3], [result4], [result5], [result6], [result7], [result8], [result9])
) AS PivotedResults

您需要使用动态SQL,因为在转动之前需要知道结果列的名称。

答案 1 :(得分:0)

你可能会尝试像

这样的东西
    INSERT INTO SomeTable
    SELECT
        (x.value * y.value) + (a.value * b.value)
    FROM
        Table1 x 
        Table1 y on y.id = x.id + 1
        Table2 a on a.id = x.id
        Table2 b on b.id = x.id + 1
    WHERE
        x.Id = @counter

答案 2 :(得分:0)

将所有记录加在一起:

insert
    NEWTABLE (theid, newval)
select
    t1_p.id as theid
    (t2_p.someval * t2_n.someval) + (t1_p.someval * t1_n.someval) as newval
from
    Table1 t1_p
inner join
    Table1 t1_n on t1_p.id + 1 = t1_n.id
inner join
    Table2 t2_p on t1_p.id = t2_p.id
inner join
    Table2 t2_n on t1_n.id = t2_n.id

答案 3 :(得分:0)

我不确定我完全理解你的问题,但......

;with cte1 as
(
  select
    T1_1.id,
    T1_1.someval as x,
    T1_2.someval as y
  from @T1 as T1_1
    inner join @T1 as T1_2
      on T1_1.id = T1_2.id-1
),
cte2 as
(
  select
    T2_1.id,
    T2_1.someval as a,
    T2_2.someval as b
  from @T2 as T2_1
    inner join @T2 as T2_2
      on T2_1.id = T2_2.id-1
)
select (a*b + x*y)
from cte1 as T1
  inner join cte2 as T2
    on T1.id = T2.id