大家COPY到写字板里面看好吗?在这里怎么都排不好!

解决方案 »

  1.   

    表APartNo LotNo LotQty  SampleQty BadQty Date CheckTimes  --第几次检查
    A 2004A 50 30 0 2004.07.10 1 --这个不属于二十九周
    B 2004B 20 10 5 2004.07.11 1
    A 2004A 60 20 0 2004.07.14 1
    C 2004B 90 30 10 2004.07.15 2
    B 2004C 30 10 0 2004.07.16 3
    C 2004D 40 10 0 2004.07.12 1
    A 2004F 40 30 0 2004.07.12 1想得到结果
    --输入时间2004.7.14 (2004年第二十九周) 首次抽检 返工抽检
    产品编号 抽检批数 接收批数 LAR% 抽检批数 接收批数 LAR% 样本总数量 抽检总批数 接收总批数 DPPM
    抽检产品数 接收产品数 抽检产品数 接收产品数 样本不良品数 抽检总数 接收总产品数A 2 2 100% 0 0 0 50 2 2 0
    100 100 0 0 0 100 100B 1 0 0 1 1 100% 20 2 1 0.25 
    20 0 30 30 5 50 30C 1 1 100% 1 0 0 40 2 1 0.25
    40 40 90 0 10 130 40每一种厂品占两行第一行:抽检批数等
    第二行:抽检产品数等以下是各数据的计算方法抽检批数 = Count(LotNo)
    接收批数 = Count(LotNo) where BadQty = 0 样本总数量 = Sum(SampleQty)
    样本不良品数 = Sum(BadQty)CheckTimes = 0 是首次检查
    CheckTimes > 0 是返工抽检LAR = 接收批数/抽检批数
    DPPM = 样本不良品数/样本总数量不知道这样够不够清楚!
      

  2.   

    不是显示格式的问题!当然我要的就是下面数据的结果!最难做的是那个DPPM,它就是要拿下一行的  样本不良品数/样本总数量
    不知道这样的结果在一个表里能否实现!
      

  3.   

    --测试--测试数据
    create table A(PartNo varchar(10),LotNo varchar(10),LotQty int,SampleQty int,BadQty int,Date datetime,CheckTimes int)
    insert A  select 'A','2004A',50,30,0 ,'2004.07.10',1
    union all select 'B','2004B',20,10,5 ,'2004.07.11',1
    union all select 'A','2004A',60,20,0 ,'2004.07.14',1
    union all select 'C','2004B',90,30,10,'2004.07.15',2
    union all select 'B','2004C',30,10,0 ,'2004.07.16',3
    union all select 'C','2004D',40,10,0 ,'2004.07.12',1
    union all select 'A','2004F',40,30,0 ,'2004.07.12',1
    go--查询
    select 产品编号=case s2 when 1 then 产品编号 else '' end
    ,首次抽检_抽检批数,首次抽检_抽检批数,[首次抽检_LAR%]
    ,返工抽检_抽检批数,返工抽检_接收批数,[返工抽检_LAR%]
    ,样本总数量,抽检总批数,接收总批数,[DPPM]=[LAR%]
    from(
    select 产品编号=PartNo
    ,首次抽检_抽检批数=sum(case when CheckTimes=0 then 1 else 0 end)
    ,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then 1 else 0 end)
    ,[首次抽检_LAR%]=case sum(case when CheckTimes=0 then 1 else 0 end) 
    when 0 then '0%'
    else cast(cast(
    sum(case when CheckTimes=0 and BadQty=0 then 100.0 else 0.0 end)
    /sum(case when CheckTimes=0 then 1 else 0 end)
    as decimal(10,2)) as varchar)+'%' end
    ,返工抽检_抽检批数=sum(case when CheckTimes>0 then 1 else 0 end)
    ,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then 1 else 0 end)
    ,[返工抽检_LAR%]=case sum(case when CheckTimes>0 then 1 else 0 end)
    when 0 then '0%'
    else cast(cast(
    sum(case when CheckTimes>0 and BadQty=0 then 100.0 else 0.0 end)
    /sum(case when CheckTimes>0 then 1 else 0 end)
    as decimal(10,2)) as varchar)+'%' end
    ,样本总数量=Sum(SampleQty)
    ,抽检总批数=sum(1)
    ,接收总批数=sum(case when BadQty=0 then 1 else 0 end)
    ,[LAR%]=case sum(1) 
    when 0 then '0%'
    else cast(cast(
    sum(case when BadQty=0 then 100.0 else 0.0 end)
    /sum(1)
    as decimal(10,2)) as varchar)+'%' end
    ,s1=PartNo,s2=1
    from A
    where datediff(week,Date,'2004.7.14')=0
    group by PartNo union all select 产品编号=PartNo
    ,首次抽检_抽检批数=sum(case when CheckTimes=0 then LotQty else 0 end)
    ,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then LotQty else 0 end)
    ,[首次抽检_LAR%]=''
    ,返工抽检_抽检批数=sum(case when CheckTimes>0 then LotQty else 0 end)
    ,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then LotQty else 0 end)
    ,[返工抽检_LAR%]=''
    ,样本不良品数=Sum(BadQty)
    ,抽检总数=sum(LotQty)
    ,接收总产品数=sum(case when BadQty=0 then LotQty else 0 end)
    ,[DPPM]=case Sum(SampleQty)
    when 0 then '0%'
    else cast(cast(
    Sum(BadQty)*100.9
    /Sum(SampleQty)
    as decimal(10,2)) as varchar)+'%' end
    ,s1=PartNo,s2=2
    from A
    where datediff(week,Date,'2004.7.14')=0
    group by PartNo
    )a order by s1,s2
    go--删除测试
    drop table A/*--测试结果(自己看)
    --*/
      

  4.   

    --测试--测试数据
    create table A(PartNo varchar(10),LotNo varchar(10),LotQty int,SampleQty int,BadQty int,Date datetime,CheckTimes int)
    insert A  select 'A','2004A',50,30,0 ,'2004.07.10',1
    union all select 'B','2004B',20,10,5 ,'2004.07.11',1
    union all select 'A','2004A',60,20,0 ,'2004.07.14',1
    union all select 'C','2004B',90,30,10,'2004.07.15',2
    union all select 'B','2004C',30,10,0 ,'2004.07.16',3
    union all select 'C','2004D',40,10,0 ,'2004.07.12',1
    union all select 'A','2004F',40,30,0 ,'2004.07.12',1
    go--查询的存储过程
    create proc p_qry
    @dt datetime --要查询的日期
    as
    select 产品编号=case s2 when 1 then 产品编号 else '' end
    ,首次抽检_抽检批数,首次抽检_抽检批数,[首次抽检_LAR%]
    ,返工抽检_抽检批数,返工抽检_接收批数,[返工抽检_LAR%]
    ,样本总数量,抽检总批数,接收总批数,[DPPM]=[LAR%]
    from(
    select 产品编号=PartNo
    ,首次抽检_抽检批数=sum(case when CheckTimes=0 then 1 else 0 end)
    ,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then 1 else 0 end)
    ,[首次抽检_LAR%]=case sum(case when CheckTimes=0 then 1 else 0 end) 
    when 0 then '0%'
    else cast(cast(
    sum(case when CheckTimes=0 and BadQty=0 then 100.0 else 0.0 end)
    /sum(case when CheckTimes=0 then 1 else 0 end)
    as decimal(10,2)) as varchar)+'%' end
    ,返工抽检_抽检批数=sum(case when CheckTimes>0 then 1 else 0 end)
    ,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then 1 else 0 end)
    ,[返工抽检_LAR%]=case sum(case when CheckTimes>0 then 1 else 0 end)
    when 0 then '0%'
    else cast(cast(
    sum(case when CheckTimes>0 and BadQty=0 then 100.0 else 0.0 end)
    /sum(case when CheckTimes>0 then 1 else 0 end)
    as decimal(10,2)) as varchar)+'%' end
    ,样本总数量=Sum(SampleQty)
    ,抽检总批数=sum(1)
    ,接收总批数=sum(case when BadQty=0 then 1 else 0 end)
    ,[LAR%]=case sum(1) 
    when 0 then '0%'
    else cast(cast(
    sum(case when BadQty=0 then 100.0 else 0.0 end)
    /sum(1)
    as decimal(10,2)) as varchar)+'%' end
    ,s1=PartNo,s2=1
    from A
    where datediff(week,Date,@dt)=0
    group by PartNo union all select 产品编号=PartNo
    ,首次抽检_抽检批数=sum(case when CheckTimes=0 then LotQty else 0 end)
    ,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then LotQty else 0 end)
    ,[首次抽检_LAR%]=''
    ,返工抽检_抽检批数=sum(case when CheckTimes>0 then LotQty else 0 end)
    ,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then LotQty else 0 end)
    ,[返工抽检_LAR%]=''
    ,样本不良品数=Sum(BadQty)
    ,抽检总数=sum(LotQty)
    ,接收总产品数=sum(case when BadQty=0 then LotQty else 0 end)
    ,[DPPM]=case Sum(SampleQty)
    when 0 then '0%'
    else cast(cast(
    Sum(BadQty)*100.9
    /Sum(SampleQty)
    as decimal(10,2)) as varchar)+'%' end
    ,s1=PartNo,s2=2
    from A
    where datediff(week,Date,@dt)=0
    group by PartNo
    )a order by s1,s2
    go--调用
    exec p_qry '2004.7.14'
    go--删除测试
    drop table A
    drop proc p_qry/*--测试结果(自己看)
    --*/
      

  5.   

    --测试数据
    create table A(PartNo varchar(10),LotNo varchar(10),LotQty int,SampleQty int,BadQty int,Date datetime,CheckTimes int)
    insert A  select 'A','2004A',50,30,0 ,'2004.07.10',1
    union all select 'B','2004B',20,10,5 ,'2004.07.11',1
    union all select 'A','2004A',60,20,0 ,'2004.07.14',1
    union all select 'C','2004B',90,30,10,'2004.07.15',2
    union all select 'B','2004C',30,10,0 ,'2004.07.16',3
    union all select 'C','2004D',40,10,0 ,'2004.07.12',1
    union all select 'A','2004F',40,30,0 ,'2004.07.12',1
    go--查询的存储过程
    create proc p_qry
    @dt datetime, @where varchar(8000)  --加了一个条件,此条件从程序中传过来
    as
    declare @s varchar(8000) --增加了一个变量,将所有的语句全存进去,因为条件不能直接接在 where 后面,我只能想到这个笨办法set @s = N'select 产品编号=case s2 when 1 then 产品编号 else '''' end
    ,首次抽检_抽检批数,首次抽检_抽检批数,[首次抽检_LAR%]
    ,返工抽检_抽检批数,返工抽检_接收批数,[返工抽检_LAR%]
    ,样本总数量,抽检总批数,接收总批数,[DPPM]=[LAR%]
    from(
    select 产品编号=PartNo
    ,首次抽检_抽检批数=sum(case when CheckTimes=0 then 1 else 0 end)
    ,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then 1 else 0 end)
    ,[首次抽检_LAR%]=case sum(case when CheckTimes=0 then 1 else 0 end) 
    when 0 then ''0%''
    else cast(cast(
    sum(case when CheckTimes=0 and BadQty=0 then 100.0 else 0.0 end)
    /sum(case when CheckTimes=0 then 1 else 0 end)
    as decimal(10,2)) as varchar)+''%'' end
    ,返工抽检_抽检批数=sum(case when CheckTimes>0 then 1 else 0 end)
    ,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then 1 else 0 end)
    ,[返工抽检_LAR%]=case sum(case when CheckTimes>0 then 1 else 0 end)
    when 0 then ''0%''
    else cast(cast(
    sum(case when CheckTimes>0 and BadQty=0 then 100.0 else 0.0 end)
    /sum(case when CheckTimes>0 then 1 else 0 end)
    as decimal(10,2)) as varchar)+''%'' end
    ,样本总数量=Sum(SampleQty)
    ,抽检总批数=sum(1)
    ,接收总批数=sum(case when BadQty=0 then 1 else 0 end)
    ,[LAR%]=case sum(1) 
    when 0 then ''0%''
    else cast(cast(
    sum(case when BadQty=0 then 100.0 else 0.0 end)
    /sum(1)
    as decimal(10,2)) as varchar)+''%'' end
    ,s1=PartNo,s2=1
    from A
    where datediff(week,Date,@dt)=0' + @where -- 这个变量新加的set @s = @s+N' group by PartNo union all select 产品编号=PartNo
    ,首次抽检_抽检批数=sum(case when CheckTimes=0 then LotQty else 0 end)
    ,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then LotQty else 0 end)
    ,[首次抽检_LAR%]=''''
    ,返工抽检_抽检批数=sum(case when CheckTimes>0 then LotQty else 0 end)
    ,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then LotQty else 0 end)
    ,[返工抽检_LAR%]=''''
    ,样本不良品数=Sum(BadQty)
    ,抽检总数=sum(LotQty)
    ,接收总产品数=sum(case when BadQty=0 then LotQty else 0 end)
    ,[DPPM]=case Sum(SampleQty)
    when 0 then ''0%''
    else cast(cast(
    Sum(BadQty)*100.9
    /Sum(SampleQty)
    as decimal(10,2)) as varchar)+''%'' end
    ,s1=PartNo,s2=2
    from A
    where datediff(week,Date,@dt)=0' + @where -- 这个也是set @s = @s+N' group by PartNo
    )a order by s1,s2'exec(@s)
    go--调用
    exec p_qry '2004.7.14', ''
    go--删除测试
    drop table A
    drop proc p_qry-- 运行后出现如下错误/*(所影响的行数为 7 行)服务器: 消息 170,级别 15,状态 1,行 1
    第 1 行: '?' 附近有语法错误。*/--大家有更好的方法吗?
      

  6.   

    --测试数据
    create table A(PartNo varchar(10),LotNo varchar(10),LotQty int,SampleQty int,BadQty int,Date datetime,CheckTimes int)
    insert A  select 'A','2004A',50,30,0 ,'2004.07.10',1
    union all select 'B','2004B',20,10,5 ,'2004.07.11',1
    union all select 'A','2004A',60,20,0 ,'2004.07.14',1
    union all select 'C','2004B',90,30,10,'2004.07.15',2
    union all select 'B','2004C',30,10,0 ,'2004.07.16',3
    union all select 'C','2004D',40,10,0 ,'2004.07.12',1
    union all select 'A','2004F',40,30,0 ,'2004.07.12',1
    go--查询的存储过程
    create proc p_qry
    @dt datetime,
    @where varchar(8000)  --加了一个条件,此条件从程序中传过来
    as
    set @where='datediff(week,Date,'''+cast(@dt as varchar)+''')=0 '
    +case isnull(@where,'') when '' then ''
    else ' and ('+@where+')' endexec('
    select 产品编号=case s2 when 1 then 产品编号 else '''' end
    ,首次抽检_抽检批数,首次抽检_抽检批数,[首次抽检_LAR%]
    ,返工抽检_抽检批数,返工抽检_接收批数,[返工抽检_LAR%]
    ,样本总数量,抽检总批数,接收总批数,[DPPM]=[LAR%]
    from(
    select 产品编号=PartNo
    ,首次抽检_抽检批数=sum(case when CheckTimes=0 then 1 else 0 end)
    ,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then 1 else 0 end)
    ,[首次抽检_LAR%]=case sum(case when CheckTimes=0 then 1 else 0 end) 
    when 0 then ''0%''
    else cast(cast(
    sum(case when CheckTimes=0 and BadQty=0 then 100.0 else 0.0 end)
    /sum(case when CheckTimes=0 then 1 else 0 end)
    as decimal(10,2)) as varchar)+''%'' end
    ,返工抽检_抽检批数=sum(case when CheckTimes>0 then 1 else 0 end)
    ,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then 1 else 0 end)
    ,[返工抽检_LAR%]=case sum(case when CheckTimes>0 then 1 else 0 end)
    when 0 then ''0%''
    else cast(cast(
    sum(case when CheckTimes>0 and BadQty=0 then 100.0 else 0.0 end)
    /sum(case when CheckTimes>0 then 1 else 0 end)
    as decimal(10,2)) as varchar)+''%'' end
    ,样本总数量=Sum(SampleQty)
    ,抽检总批数=sum(1)
    ,接收总批数=sum(case when BadQty=0 then 1 else 0 end)
    ,[LAR%]=case sum(1) 
    when 0 then ''0%''
    else cast(cast(
    sum(case when BadQty=0 then 100.0 else 0.0 end)
    /sum(1)
    as decimal(10,2)) as varchar)+''%'' end
    ,s1=PartNo,s2=1
    from A
    where '+@where+'
    group by PartNo union all select 产品编号=PartNo
    ,首次抽检_抽检批数=sum(case when CheckTimes=0 then LotQty else 0 end)
    ,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then LotQty else 0 end)
    ,[首次抽检_LAR%]=''''
    ,返工抽检_抽检批数=sum(case when CheckTimes>0 then LotQty else 0 end)
    ,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then LotQty else 0 end)
    ,[返工抽检_LAR%]=''''
    ,样本不良品数=Sum(BadQty)
    ,抽检总数=sum(LotQty)
    ,接收总产品数=sum(case when BadQty=0 then LotQty else 0 end)
    ,[DPPM]=case Sum(SampleQty)
    when 0 then ''0%''
    else cast(cast(
    Sum(BadQty)*100.9
    /Sum(SampleQty)
    as decimal(10,2)) as varchar)+''%'' end
    ,s1=PartNo,s2=2
    from A
    where '+@where+'
    group by PartNo
    )a order by s1,s2
    ')
    go--调用
    exec p_qry '2004.7.14', 'PartNo=''A'''
    go--删除测试
    drop table A
    drop proc p_qry--结果自己看