【发布时间】:2015-03-10 17:45:51
【问题描述】:
我有一个链接数据库,并创建了一份报告,总结了在指定日期范围内测试的样本:产品、产品组、产品描述、测试描述 - 有多少人通过/失败了该测试。一切都很好,除了我需要计算每个产品组测试的样本数量而不是测试数量——这是当前输出的。我有一个查询,它分析结果并分配通过/失败指定,这些都被计算在内;我还创建了一个查询,该查询计算提交的样本数量(也在日期范围之间),但无法弄清楚如何将其合并到用于报告的主查询中。 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;
Count Lab Samples 查询副本:
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));
【问题讨论】:
标签: database join count distinct