将多个表中的数据插入临时表

时间:2011-12-09 09:49:11

标签: sql-server

--Receive_Amount Having Column
________________________________________________
PID    Head             Receive_Date    Amount
------------------------------------------------
1001   consumeable       12/8/2011        20,000
1001   Manpower          12/8/2011      1,00,000
1001   Travel            13/8/2011        40,000
1001   Travel            18/8/2011        10,000
1001   consumeable       5/09/2011        10,000

--Second Table
--Expenditure Table
PID    Head             Exp_Date    Amount
------------------------------------------------
1001   Travel           13/8/2011    2,000
1001   consumeable      13/8/2011    5,000
1001   Travel           18/08/2011   1,000

--And Third Table
--Advance Table Having Column
_____________________________________________
PID    Head             Adv_Date    Amount
---------------------------------------------
1001   Travel           14/08/2011   3,000

我想将这3个表中的数据插入临时表

___________________________________________________________________________
PID  Trans_Date   Head       Head_Balance     Exp_Amount    Running Total
___________________________________________________________________________
1001   13/8/2011  Travel        40,000         2,000           2,000
1001   13/8/2011  consumeable   20,000         5,000           7,000
1001   18/08/2011 Travel        48,000         1,000           8,000

1 个答案:

答案 0 :(得分:0)

如果不同的表看起来相同,则可以使用union

select * from Receive_Date Amount
union all
select * from Exp_Date Amount
union all
select * from Adv_Date Amount