Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rajesh_
Starting Member
8 Posts |
Posted - 2007-06-11 : 08:30:33
|
HI, I have two tables A and B with following DataTable A------------ID(PK) | V | W | X | Final----------------------1 | 4 | 5 | 6 | outputTable B------------ID | Par_ID (FK) | Cost_Per_Annum------------------------ 1 | 1 | 12000 2 | 1 | 24000 3 | 1 | 14000Output should be calculated using below the formula .output = Column V * 1st record of par_id=1 + Column w * 2nd record of par_id=1 + Column X * 3rd record of par_id=1output = 4* 12000 + 5* 24000 + 6 * 14000How can i do like this i have tried several ways and failed. Please suggest me. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-11 : 08:48:55
|
[code]select a.ID, a.V, a.W, a.X, Final = (a.V * b1.Cost_Per_Annum) + (a.W * b2.Cost_Per_Annum) + (a.X * b3.Cost_Per_Annum)from TableA ainner join TableB b1 on a.ID = b1.Par_ID and b1.ID = 1inner join TableB b2 on a.ID = b2.Par_ID and b2.ID = 2inner join TableB b3 on a.ID = b3.Par_ID and b3.ID = 3[/code] KH |
 |
|
rajesh_
Starting Member
8 Posts |
Posted - 2007-06-11 : 23:10:34
|
this is not dynamic khtan. You are taking ID values as 1,2,3. What if i have many records?. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-11 : 23:14:04
|
quote: Originally posted by rajesh_ this is not dynamic khtan. You are taking ID values as 1,2,3. What if i have many records?.
Well you did not say so.Please state the full requirement, scenario, post more sample data and the expected result KH |
 |
|
rajesh_
Starting Member
8 Posts |
Posted - 2007-06-11 : 23:46:13
|
Table A------------ID(PK) | V | W | X | Final----------------------1 | 4 | 5 | 6 | output2 | 5 | 6 | 7 | output3 | 4 | 9 | 10| outputTable B------------ID | Par_ID (FK) | Cost_Per_Annum------------------------1 | 1 | 120002 | 1 | 240003 | 1 | 140004 | 2 | 100005 | 2 | 310006 | 2 | 420007 | 3 | 120008 | 3 | 140009 | 3 | 12000Output should be calculated using below the formula .For par_id = 1--------------output = Column V * 1st record of par_id=1 + Column w * 2nd record of par_id=1 + Column X * 3rd record of par_id=1Same way for par_id=2 and 3 continuesoutput for (ID =1) = 4* 12000 + 5* 24000 + 6 * 14000output for (ID =2) = 5* 10000 + 6* 31000 + 7 * 42000output for (ID =3) = 4* 12000 + 9* 14000 + 10 * 12000I given total 3 samples. In real time they may be many records.Thanks khtan |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-12 : 00:22:21
|
[code]declare @TableA table( ID int, V int, W int, X int, Final int)insert into @TableA (ID, V, W, X)select 1, 4, 5, 6 union allselect 2, 5, 6, 7 union allselect 3, 4, 9, 10declare @TableB table( ID int, Par_ID int, Cost_Per_Annum int)insert into @TableBselect 1, 1, 12000 union allselect 2, 1, 24000 union allselect 3, 1, 14000 union allselect 4, 2, 10000 union allselect 5, 2, 31000 union allselect 6, 2, 42000 union allselect 7, 3, 12000 union allselect 8, 3, 14000 union allselect 9, 3, 12000select a.ID, a.V, a.W, a.X, Final = sum ( (a.V * case when row = 1 then b.Cost_Per_Annum else 0 end) + (a.W * case when row = 2 then b.Cost_Per_Annum else 0 end) + (a.X * case when row = 3 then b.Cost_Per_Annum else 0 end) )from @TableA a inner join ( select ID, Par_ID, row = (select count(*) from @TableB x where x.Par_ID = b.Par_ID and x.ID <= b.ID), Cost_Per_Annum from @TableB b )b on a.ID = b.Par_IDgroup by a.ID, a.V, a.W, a.X/*ID V W X Final ----------- ----------- ----------- ----------- ----------- 1 4 5 6 252000 2 5 6 7 530000 3 4 9 10 294000 */[/code] You should have another column in TableB that identifies which row is for V or W or X. KH |
 |
|
rajesh_
Starting Member
8 Posts |
Posted - 2007-06-12 : 01:45:02
|
thank you so much khtan |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-12 : 01:50:20
|
Do take not of my last comment. Currently the query is assuming that the rows in TableB are in ascending order for V, W & X. Which may not be valid any more if you add / delete records from the table. With the additional column in TableB to identify the corresponding records for V, W or X, the query can be re-written more efficiently. KH |
 |
|
rajesh_
Starting Member
8 Posts |
Posted - 2007-06-12 : 02:52:26
|
I got it. I modified query. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-12 : 09:36:50
|
Why did you design Matrix like table structure?If you have V,W and X columns in TableB, then that would be too simple to get the result you wantIt is just multiplying respective columnsMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|