查询以计算传递/失败的测试数量的唯一样本ID

时间:2015-03-10 17:45:52

标签: database join count distinct

我有一个链接数据库,并创建了一个报告,汇总了在指定日期范围内测试的样本:产品,产品组,产品描述,测试描述 - 测试通过/失败的数量。一切正常,除了我需要计算每个产品组测试的样本数量而不是测试数量 - 这是当前输出的数量。我有一个查询分析结果并分配通过/失败指定,并计算这些;我还创建了一个查询,计算提交的样本数量(也在日期范围之间),但无法弄清楚如何将其合并到用于报告的主查询中。 sum函数加起来测试的次数,但我需要样本的数量(即一个样本可以有5个不同的测试,并且可能只传递其中的2个)。我很难将2 + 2放在一起! I've attached an image of my report design to help visualize

LabResultsAnalysisCountPass-Fail查询的副本:

SELECT DISTINCTROW [Copy of LabResultsAnalysisWithPass Query].Name, [Copy of    LabResultsAnalysisWithPass Query].Plant, [Copy of LabResultsAnalysisWithPass Query].ProductGroup, [Copy of LabResultsAnalysisWithPass Query].ProductDescription, [Copy of LabResultsAnalysisWithPass Query].TestDescription, [Copy of LabResultsAnalysisWithPass Query].Pass, Count([Copy of LabResultsAnalysisWithPass Query].Pass) AS CountOfPass
FROM [Copy of Count Lab Samples Query], LabProductTest INNER JOIN [Copy of LabResultsAnalysisWithPass Query] ON LabProductTest.LabProductTestID = [Copy of LabResultsAnalysisWithPass Query].LabProductTestID
GROUP BY [Copy of LabResultsAnalysisWithPass Query].Name, [Copy of LabResultsAnalysisWithPass Query].Plant, [Copy of LabResultsAnalysisWithPass Query].ProductGroup, [Copy of LabResultsAnalysisWithPass Query].ProductDescription, [Copy of LabResultsAnalysisWithPass Query].TestDescription, [Copy of LabResultsAnalysisWithPass Query].Pass, LabProductTest.Deleted
HAVING (((LabProductTest.Deleted)=False));

LabResultsAnalysisWithPass查询的副本:

PARAMETERS [Enter Lab Results start date] DateTime;
SELECT PlantInformation.Name, LabPlantSample.Plant, Product.ProductDescription, ProductGroup.ProductGroupCode, ProductGroup.ProductGroup, LabTestProvincialStandard.TestDescription, LabTestProvincialStandard.Standard,                IIf([ResultTxt]="NEGATIVE","Passed",IIf([ResultTxt]="POSITIVE","Failed",IIf([Standard]="NEGATIVE" And [ResultTxt]<>"NEGATIVE" And [ResultPrefix]<>"<","Failed",IIf([Standard]="NEGATIVE" And (IsNumeric([ResultTxt])<="10" And [ResultPrefix]="<"),"Passed",IIf(CCur([ResultTxt])<=(CCur([Standard])) And (IsNull([ResultPrefix]) Or [ResultPrefix]="<" Or [ResultPrefix]=""),"Passed",IIf(IsNumeric([ResultTxt])<=10 And [ResultPrefix]="<","Passed",IIf(IsNumeric([ResultTxt])>(IsNumeric([Standard])) And (IsNull([ResultPrefix]) Or [ResultPrefix]="<" Or [ResultPrefix]=""),"Failed",IIf(CCur([ResultTxt])>(CCur([Standard])) And (IsNull([ResultPrefix]) Or [ResultPrefix]=">"),"Failed","Failed")))))))) AS Pass, LabProductSample.LabProductSampleID, LabProductTest.LabProductTestID, LabPlantSample.SampleDate
FROM PlantInformation INNER JOIN (LabPlantSample INNER JOIN (((LabTestProvincialStandard INNER JOIN (Product INNER JOIN ProductGroup ON Product.ProductGroupCode = ProductGroup.ProductGroupCode) ON LabTestProvincialStandard.ProductGroupCode = ProductGroup.ProductGroupCode)     INNER JOIN LabProductTest ON LabTestProvincialStandard.TestDescription = LabProductTest.TestDescription) INNER JOIN LabProductSample ON (LabProductSample.LabProductSampleID = LabProductTest.LabProductSampleID) AND (Product.ProductDescription = LabProductSample.ProductDescription)) ON LabPlantSample.PlantSampleID = LabProductSample.PlantSampleID) ON PlantInformation.Plant = LabPlantSample.Plant
WHERE (((LabPlantSample.SampleDate)>=[Enter Lab Results start date]) AND ((LabProductTest.Deleted)=False) AND ((LabProductSample.Deleted)=False) AND ((LabPlantSample.Deleted)=False) AND ((PlantInformation.Inactive)=False))
ORDER BY PlantInformation.Name, ProductGroup.ProductGroupCode, ProductGroup.ProductGroup, LabTestProvincialStandard.TestDescription;

计数实验室样本查询的副本:

PARAMETERS [Enter Lab Results start date] DateTime, [Enter Lab Results end date] DateTime;
SELECT PlantInformation.Plant, PlantInformation.Name, Count(LabProductSample.LabProductSampleID) AS CountOfLabProductSampleID, LabPlantSample.SampleDate
FROM PlantInformation INNER JOIN (LabPlantSample INNER JOIN LabProductSample ON LabPlantSample.PlantSampleID = LabProductSample.PlantSampleID) ON PlantInformation.Plant = LabPlantSample.Plant
GROUP BY PlantInformation.Plant, PlantInformation.Name, LabPlantSample.SampleDate, LabProductSample.Deleted
HAVING (((LabPlantSample.SampleDate) Between [Enter Lab Results start date] And [Enter Lab Results end date]) AND ((LabProductSample.Deleted)=False));

1 个答案:

答案 0 :(得分:0)

也许最简单的解决方案是在报告中输出传递的测试次数,失败的测试次数和样本数作为单独的列。

如果这是可以接受的,你可以添加一个通过次数,并且计数失败,并且#34; LabResultsAnalysisWithPass的复制查询&#34; (并删除现有的&#34; pass&#34;列)。然后在&#34; LabResultsAnalysisCountPass-Fail&#34;的复制中,对通过求和,对失败求和并计算&#34; LabProductSampleID&#34;。你根本不需要最后一个查询。