0

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

asked Feb 28, 2011 at 22:17
4
  • Why do you want the results horizontal? That's better handled by the application than by the database layer. Commented Feb 28, 2011 at 22:51
  • well, the reason is that I will insert results the same way as described so, I will get a table with 100 columns and n rows, where a row is calculated as described Commented Feb 28, 2011 at 22:53
  • The code you provided only calculates a single row. Do you mean you will repeat EXEC(@template) over and over until you are down to a single value? Commented Feb 28, 2011 at 23:06
  • Not EXEC(@template), template will changue and take other values from other tables and keep inserting in that new table Commented Feb 28, 2011 at 23:39

4 Answers 4

1

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.

answered Mar 1, 2011 at 0:21

Comments

0

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
answered Feb 28, 2011 at 22:31

3 Comments

So if Now I have to do this 100 times and insert each of this results into a new table, How does the query look like?? Dynamic SQL?
@darkcminor: No, you would only run this query once. It does the operation for all of the (100+) rows in a single query. Updated with an example how you would insert.
@darkcminor: Can you provide an example output what NEWTABLE should look like? Then I can give a more specific answer as well.
0

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
answered Feb 28, 2011 at 22:51

Comments

0

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
answered Feb 28, 2011 at 22:31

1 Comment

One question...In your query how does @counter variable is incremented, and if lets say @counter depends on another variable???

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.