我有这个查询:select distinct pdi.height, pdi.width, pj.jobnum , (select count(barcode) from productiondoorinformation inner join view_productionjobs on view_productionjobs.ctr=productiondoorinformation.productionjobsctr where view_productionjobs.jobnum=pj.jobnum and productiondoorinformation.height=pdi.height and productiondoorinformation.width=pdi.width and productiondoorinformation.alternaterating='PARTICLE') as particleqty , (select count(barcode) from productiondoorinformation inner join view_productionjobs on view_productionjobs.ctr=productiondoorinformation.productionjobsctr where view_productionjobs.jobnum=pj.jobnum and productiondoorinformation.height=pdi.height and productiondoorinformation.width=pdi.width and productiondoorinformation.alternaterating<>'PARTICLE') as laminatedqty from productiondoorinformation pdi inner join view_productionjobs pj on pj.ctr=pdi.productionjobsctr where pj.jobnum='' + @Jobnum + ''必须有更好的方法才能做到这一点。我讨厌子选择,但是不够精明,无法在没有它们的情况下重新编写它。当您看到每个子句的where子句时,子选择计数的数量有两种。该查询需要九秒钟来执行。太久了我针对这些表编写的任何其他查询都将立即返回。有什么建议?我尝试了group by等,但是只能使其以一种或另一种方式(粒子或粒子)工作,但不能同时工作。返回的数据应如下所示:height | width | jobnum | particleqty | laminatedqty79 49 t1000 10 578 49 t1000 1 379 47 t1000 15 0 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您在正确的轨道上。您应该能够使用SUM代替COUNT和GROUP BY:select pdi.height, pdi.width, pj.jobnum, SUM(CASE pdi.alternaterating WHEN 'PARTICLE' THEN 1 ELSE 0 END) particleqty, SUM(CASE pdi.alternaterating WHEN 'PARTICLE' THEN 0 ELSE 1 END) laminatedqtyfrom productiondoorinformation pdi inner join view_productionjobs pj on pj.ctr=pdi.productionjobsctr where pj.jobnum='' + @Jobnum + ''group by pdi.height, pdi.width, pj.jobnum顺便说一句,不确定您是否需要@Jobnum周围的那些单引号('')... (adsbygoogle = window.adsbygoogle || []).push({}); 10-06 10:11