日期:2014-05-17 浏览次数:20549 次
CREATE TABLE t_Job
(
班组ID INT,
员工 VARCHAR(10),
白班或夜班 VARCHAR(10),
合格品数量 int,
总产品数量 int,
rq date
)
insert into t_Job
select 1,'张三','白班',90,100,'2013/1/1'
union all
select 1,'张三','夜班',80,100,'2013/1/1'
union all
select 1,'李四','夜班',90,100,'2013/1/1'
union all
select 1,'王五','白班',96,100,'2013/1/2'
union all
select 1,'张三','夜班',83,100,'2013/1/2'
union all
select 1,'李四','夜班',83,100,'2013/1/1'
union all
select 2,'sam','白班',186,200,'2013/1/1'
union all
select 2,'sam','夜班',160,200,'2013/1/1'
union all
select 2,'john','夜班',160,200,'2013/1/1'
union all
select 2,'mike','白班',176,200,'2013/1/2'
union all
select 2,'sam','夜班',166,200,'2013/1/2'
union all
select 2,'john','夜班',166,200,'2013/1/2'
select * from t_Job;
select tm.* from
(
select 员工,cast(SUM(合格品数量) as decimal)/SUM(总产品数量) 合格率, DATEPART(m,rq) ToMonth from t_Job group by [员工], DATEPART(m,rq)) tm
order by tm.合格率 desc
/*
员工 合格率 ToMonth
---------- --------------------------------------- -----------
王五 0.96000000000 1
mike 0.88000000000 1
李四 0.86500000000 &