日期:2014-05-17 浏览次数:20581 次
if object_id('tb') is not null drop table tb
go
create table tb(BarCode varchar(30),TestTime datetime,Result varchar(10))
insert into tb(BarCode,TestTime,Result)
select 'DYD3282AT93FD0785','2013-07-09 16:20','PASS' union all
select 'DYD3283AK6NFD0782','2013-07-09 16:21','PASS' union all
select 'DYD3283AK6TFD078X','2013-07-09 16:22','PASS' union all
select 'DYD3283AK6SFD078Y','2013-07-09 16:23','PASS' union all
select 'DYD3283AK6MFD0783','2013-07-09 16:23','PASS' union all
select 'DYD3283AK6QFD0780','2013-07-09 16:24','PASS' union all
select 'DYD3283AK6EFD078A','2013-07-09 16:24','PASS' union all
select 'DYD3283AK6FFD0789','2013-07-09 16:25','PASS' union all
select 'DYD3283AK6JFD0786','2013-07-09 16:26','PASS' union all
select 'DYD3283AK6KFD0785','2013-07-09 16:26','PASS' union all
select 'DYD3282AT91FD0787','2013-07-09 16:28','PASS' union all
select 'DYD3283BKEWFD0781','2013-07-09 16:28','PASS' union all
select 'DYD3282BTBSFD0787','2013-07-09 16:29','PASS' union all
select 'DYD3283AL1MFD078H','2013-07-09 16:30','PASS' union all
select 'DYD3282AU3AFD078F','2013-07-09 16:31','FAIL' union all
select 'DYD3283BK4HFD078A','2013-07-09 16:31','PASS' union all
select 'DYD3272AU8EFD078Z','2013-07-16 14:48','PASS' union all
select 'DYD3272AT8GFD078Y','2013-07-16 14:48','FAIL' union all
select 'DYD3272AT24FD078U','2013-07-16 14:49','PASS' union all
select 'DYD3272AT7RFD078S','2013-07-16 14:49','FAIL' union all
select 'DYD3272AT1XFD0784','2013-07-16 14:51','FAIL' union all
select 'DYD3272AT4MFD0785','2013-07-16 14:51','FAIL' union all
select 'DYD3272AT4QFD0782','2013-07-16 14:52','FAIL' union all
select 'DYD3272AT89FD0785','2013-07-16 15:14','FAIL'
go
--测试语句
;with cte as(
select [时间段]=CONVERT(char(15),TestTime,120)+'0'
,[产量]=COUNT(*)
,[合格数量]=SUM(case when result='PASS' then 1 else 0 end)
from tb with(nolock)
group by CONVERT(char(15),TestTime,120)+'0'
)select *,CONVERT(numeric(5,2),[合格数量]*1.0/[产量]) as [良率] from cte
go
drop table tb