开发者

dynamic operations with 2 tables and 1 insert SQL Server

I have 2 tables:

table1

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

table2

    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

I need to insert to a new table some operations of various fields of both tables so, for example

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

Then insert into NEWTABLE

(a*b + x+y), (r*s + u*v) ...(q*p+o*e) 

sO I was thinking (IF I have to do this 100 times):

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

so at the end of the loop I have a full template

and then EXEC(@template) ,

  • How could you optimize this?
  • How to do something like I described? (table1 and table2 are samll examples of my tables)
  • Is there a way The 4 select statements are avoided?

output table:

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

where float is a result of calculating (a*b + x+y) for each value in table1 and table2


This will work with SQL 2005 & up, using the same data you listed.

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

If you want it horizontal, you'll have to PIVOT the results.

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

You'll need to use dynamic SQL for this, because you need to know the names of your result colums before you pivot.


Join all the records together:

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


Not sure I understand your question fully but....

;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


Might you try something like

    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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜