将两个表记录合并为一个

时间:2013-12-03 12:48:25

标签: sql sql-server sql-server-2008 sql-server-2005

我有关于合并两个数据表的问题。我有两个表Table1和Table2,在表1中有两列和10行,在表2中有5行和1列。现在我能够找到合并和计算所需结果的解决方案

Table1

SNO - Name  -  Amount 
1   -   A   -   10
2   -   B   -   20
3   -   C   -   30
4   -   D   -   40
5   -   E   -   50
6   -   F   -   60
7   -   G   -   70
8   -   H   -   80
9   -   I   -   90
10  -   J   -   100

Table2

Rate(%)  
  20   
  30   
  50   
  60   
  10 
Now I want result like this

New_Amount Calculation -> (Amount*Rate)/100)

   SNO  - Name  -  Amount -  New_Amount 
    1   -   A   -   10    -    2
    2   -   B   -   20    -    6
    3   -   C   -   30    -    15
    4   -   D   -   40    -    24
    5   -   E   -   50    -    20
    6   -   F   -   60    -    12
    7   -   G   -   70    -    21
    8   -   H   -   80    -    24
    9   -   I   -   90    -    54
    10  -   J   -   100    -   10

此处SQLFiddle

NOte: There no relation between both table

1 个答案:

答案 0 :(得分:1)

我真的没有看到你如何获得SNO 6的可能Rate - > 10,但看起来简单的连接就足够了:

select
    t1.SNO,
    t1.Name,
    t1.Amount,
    New_Amount = (t1.Amount * t2.Rate) / 100
from Table1 t1
inner join Table2 t2
on t1.SNO = t2.SNO;