有两个表,用户id一一对应,
表1
id name 表现分
1 王小二 85
23 章小品 90
33 郭培育 75表2
id name 绩效分
1 王小二 90
23 章小品 85
33 郭培育 100要求每个人的总分=表现分*0.3+绩效分*0.7
求一条插入语句,插入表3
表3
id 总分
各位大侠帮忙哦!!!
谢谢!
表1
id name 表现分
1 王小二 85
23 章小品 90
33 郭培育 75表2
id name 绩效分
1 王小二 90
23 章小品 85
33 郭培育 100要求每个人的总分=表现分*0.3+绩效分*0.7
求一条插入语句,插入表3
表3
id 总分
各位大侠帮忙哦!!!
谢谢!
insert tb3
select id,总分=0.3*a.表现分+0.7*b.绩效分
from tb1 a join tb2 b on a.id=b.id
ISNULL(a.score,0) +ISNULL(b.score,0)
FROM (
SELECT ID,SUM(表现分*0.3) as score
FROM 表1
GROUP BY ID
) a
FULL OUTER JOIN
(
SELECT ID,SUM(绩效分*0.7) as score
FROM 表2
GROUP BY ID
) b
ON a.ID=b.ID
insert into #table1
select 1,'王小二',85 union all
select 23,'章小品',90 union all
select 33 ,'郭培育',75 union
--注意是一条例外数据,44不在绩效分中!
select 44 ,'exception',60
create table #table2( id int,name nvarchar(50),绩效分 decimal(10,1))
insert into #table2
select 1,'王小二',90 union all
select 23,'章小品',85 union all
select 33 ,'郭培育',100
create table #table3( id int,name nvarchar(50),总分 decimal(10,1))insert into #table3
select
t_name.id,
t_name.name,
isnull(表现分,0) * 0.3 + isnull(绩效分,0) * 0.7 总分
from
(
select id,name from #table1
union
select id,name from #table2
)t_name
left outer join #table1 on #table1.id =t_name.id
left outer join #table2 on #table2.id =t_name.idselect * from #table3
drop table #table1
drop table #table2
drop table #table3结果:
id name 总分
1 王小二 88.5
23 章小品 86.5
33 郭培育 92.5
44 exception 18.0
insert into #table1
select 1,'王小二',85 union all
select 23,'章小品',90 union all
select 33 ,'郭培育',75 union
--注意是一条例外数据,44不在绩效分中!
select 44 ,'exception',60
create table #table2( id int,name nvarchar(50),绩效分 decimal(10,1))
insert into #table2
select 1,'王小二',90 union all
select 23,'章小品',85 union all
select 33 ,'郭培育',100
create table #table3( id int,name nvarchar(50),总分 decimal(10,1))insert into #table3
select
t_name.id,
t_name.name,
isnull(表现分,0) * 0.3 + isnull(绩效分,0) * 0.7 总分
from
(
select id,name from #table1
union
select id,name from #table2
)t_name
left outer join #table1 on #table1.id =t_name.id
left outer join #table2 on #table2.id =t_name.idselect * from #table3
drop table #table1
drop table #table2
drop table #table3结果:
id name 总分
1 王小二 88.5
23 章小品 86.5
33 郭培育 92.5
44 exception 18.0