一个sql我不知道怎么写 请大家帮帮我(50分)

D

dyh506

Unregistered / Unconfirmed
GUEST, unregistred user!
有12个工资表分别是1到12月的工资,每个表的结构都一样,其中一个是字段是平均工资,
为null,(它的值是每个月工资的平均。)我想在表12中,也就是在12月工资表中把平均工资
写进表里,sql怎么写?
 
全年的平均工资吗?
 
update 工资表12 set 平均工资=工资+津贴+...+/工资项
 
是全年的啊 平均工资=(1月+2月...+12月)/12 就象下面拿2个表来举例
一月 2月
id 工资 平均工资 id 工资 平均工资
1 100 1 200
2 200 2 300
执行玩sql后2月中的数据
id 工资 平均工资
1 200 150 (100+200)/2
2 300 250(200+300)/2
请问这个sql怎么写!
 
insert into tmp_table select 工资表1.工资 as gz1,工资表2.工资 as gz2,....,工资表12.工资12 as gz12 from 工资表1,工资表2,...工资表12
select sum (gz1+gz2+...+gz12) as gz_toalfrom tmp_table

update 工资表12 set 平均工资=gz_toal/12
 
tmp_table为临时表
 
to chestermao, 好象不行啊 小弟愚笨 请给出完整代码!!
 
--二月份的
update table2 set avg_salary =
( select a.salary + b.salary from table1 a, table2 b
where a.id=table2.id and b.id = table2.id )
--三月分
update table3 set avg_salary =
( select a.salary + b.salary+c.salary from table1 a, table2 b,table3 c
where a.id=table3.id and b.id = table3.id and c.id = table3.id )
--如此等等
最好的建一个动态的视图view,就更加简洁的sql语句了.
比如计算二月的时候,create view view_all
as select * from table1 union * from table2
update table2 set avg_salary = ( select sum(salary) from view_all where id= table2.id);
三月
create view view_all
as select * from table1 union select * from table2 union select * from table3
update table3 set avg_salary = ( select sum(salary) from view_all where id= table3.id);
如此类推继续
十二月
create view view_all
as
select * from table1
union select * from table2
union select * from table3
union select * from table4
union select * from table5
union select * from table6
union select * from table7
union select * from table8
union select * from table9
union select * from table10
union select * from table11
union select * from table12

update table12 set avg_salary = ( select sum(salary) from view_all where id= table12.id);

我用的是oracle 语法,相应的mssql语句应该差不多,可能需要稍微改造

 
顶部