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 |
dmilam
Posting Yak Master
185 Posts |
Posted - 2012-11-02 : 13:40:55
|
I have a table, call it A, that has a column of integers.A.RowID A.Value1 102 203 304 405 50I have another table, call it B, also with a column of integers.B.RowID B.Value3 604 705 80I want to divide each B.Value by its corresponding A.Value (a.RowID = b.RowID) without each value of B being divided by only the first value of A, if that makes sense -- which is what I think a subquery in SELECT will do. |
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2012-11-02 : 13:59:16
|
INNER JOINing the two sets and then dividing seems to work, but if there are other solutions, I'd like to see them.SELECT b.*, a.*, (1.* b.Value / a.Value) as prcntFROM (SELECT b.Value, b.IDFROM B as b)INNER JOIN(SELECT a.Value, a.IDFROM A as a) ON a.ID = b.ID |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-11-02 : 14:21:36
|
This is equivalent and simpler:SELECT b.*, a.*, (1.* b.Value / a.Value) as prcntFROM B as bINNER JOIN A as a ON a.ID = b.ID |
 |
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2012-11-02 : 14:48:49
|
Very nice, always like simpler! Thanks. This will go within some dynamic SQL and incorporate other columns, but it's good to see a foundation to work from. quote: Originally posted by robvolk This is equivalent and simpler:SELECT b.*, a.*, (1.* b.Value / a.Value) as prcntFROM B as bINNER JOIN A as a ON a.ID = b.ID
|
 |
|
|
|
|
|
|