select distinct 'CSP','PU4','BOI',a.line,a.cust_model,'20061009','D',substring(c.comp_format,5,2),
count(case when a.errcode='PASS' and a.station='FFT'  then a.serial_number else null end) as PassQty, 
count(case when a.station='CSP'then a.serial_number else null end) as CSPQty
from pqc a,[172.26.40.20].Report.dbo.FQA_Err_Information b,comp_format c
WHERE a.transdatetime between  '20061009200000' and '20061010080000' and a.cust_model=c.qci_model and c.category='MB'
group by a.line,a.cust_model,substring(c.comp_format,5,2)
order by a.line ,a.cust_model,substring(c.comp_format,5,2)
我在计算count的时候,为什么comp_format起了作用,得到了不是我想要的结果
我要在这个table里面抓东西,又不能把这个table去掉
请教高手,如何解决?

解决方案 »

  1.   

    --把distinct去掉,然後用sum看看,count(列名)本身會排除null
    select  'CSP','PU4','BOI',a.line,a.cust_model,'20061009','D',substring(c.comp_format,5,2),
    sum(case when a.errcode='PASS' and a.station='FFT'  then 1 else 0 end) as PassQty, 
    sum(case when a.station='CSP' then 1 else 0 end) as CSPQty
    from pqc a,[172.26.40.20].Report.dbo.FQA_Err_Information b,comp_format c
    WHERE a.transdatetime between  '20061009200000' and '20061010080000' and a.cust_model=c.qci_model and c.category='MB'
    group by a.line,a.cust_model,substring(c.comp_format,5,2)
    order by a.line ,a.cust_model,substring(c.comp_format,5,2)
      

  2.   

    select distinct 'CSP','PU4','BOI',a.line,a.cust_model,'20061009','D',substring(c.comp_format,5,2),
    count(case when a.errcode='PASS' and a.station='FFT'  then a.serial_number else null end) as PassQty, 
    --*************************
    count(case when a.station='CSP'then a.serial_number else null end) as CSPQty
    这一句是不是没有统计出含null的数据。改成:
    count(case when a.station='CSP'then a.serial_number else "" end) as CSPQty
    试试看。
    --*************************
    from pqc a,[172.26.40.20].Report.dbo.FQA_Err_Information b,comp_format c
    WHERE a.transdatetime between  '20061009200000' and '20061010080000' and a.cust_model=c.qci_model and c.category='MB'
    group by a.line,a.cust_model,substring(c.comp_format,5,2)
    order by a.line ,a.cust_model,substring(c.comp_format,5,2)
      

  3.   

    数据不对,而且效率狂低我要count table [a] 里面的数据, 
    count(case when a.errcode='PASS' and a.station='FFT'  then a.serial_number else null end)
    我没有让a和c有link关系,但c还是起作用了
      

  4.   

    注意一下COUNT使用的情况就行了==================================
    在SQL Server 高手的大海中小心的行走
    ==================================
      

  5.   

    table c 让我的数据变的多了很多
      

  6.   

    那把count單獨出來好了
    declare @n int,@m int
    set @n=(select count(serial_number) from pqc where errcode='PASS' and station='FFT')
    set @m=(select count(serial_number) from pqc where station='CSP')select distinct 'CSP','PU4','BOI',a.line,a.cust_model,'20061009','D',substring(c.comp_format,5,2),
    @n as PassQty, 
    @m as CSPQty
    from pqc a,[172.26.40.20].Report.dbo.FQA_Err_Information b,comp_format c
    WHERE a.transdatetime between  '20061009200000' and '20061010080000' and a.cust_model=c.qci_model and c.category='MB'
    group by a.line,a.cust_model,substring(c.comp_format,5,2)
    order by a.line ,a.cust_model,substring(c.comp_format,5,2)