日期:2014-05-16 浏览次数:20637 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-29 09:28:52
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([序号] int,[完成人] varchar(3),[日期] varchar(4),[完成效率] int,[完成质量] int,[难易度] int,[组号] varchar(1))
insert [a]
select 1,'王x','2-11',3,5,6,'a' union all
select 2,'李x','2-12',4,7,8,'a'
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([评分序号] int,[组号] varchar(1),[评分项目] varchar(8),[评分比例] numeric(2,1))
insert [b]
select 1,'a','完成效率',0.2 union all
select 2,'a','完成质量',0.4 union all
select 3,'a','难易度',0.4 union all
select 4,'b','x',0
--------------开始查询--------------------------
SELECT
a.序号,a.完成人,a.日期,a.完成效率,a.完成质量,a.难易度,
SUM(CASE WHEN b.评分项目='完成效率' THEN a.完成效率*b.评分比例 ELSE 0 END)+
SUM(CASE WHEN b.评分项目='完成质量' THEN a.完成质量*b.评分比例 ELSE 0 END)+
SUM(CASE WHEN b.评分项目='难易度' THEN a.难易度*b.评分比例 ELSE 0 END) AS 总分
FROM a INNER JOIN b ON a.组号=b.组号
GROUP BY
a.序号,a.完成人,a.日期,a.完成效率,a.完成质量,a.难易度
----------------结果-----