R
Randall Arnold
I have a SQL query that builds a list of shipping order errors. Each error
report has one Purchase Order Number (PON) but may have many errors
associated with that order. I aggregate errors based on Description and get
totals of each type. Date ranges are automatically filtered based on dates
on a user form. I can also filter based on Product Type and Area of
Responsibility off of this same user form. This part of my query works as
expected. I have a chart on a report that utilizes this query and it also
functions as desired.
However, I also need to determine the total number of unique purchase orders
and have that number show up in the query rows. I need this because I want
to be able to show, row by row, how many defects occurred out of how many
unique purchase orders (ie, opportunities for error).
I have tried numerous ways to get this to work and all have failed.
Microsoft Access does not seem to follow SQL convention 100% in that I should
be able to use a DISTINCT aggregate with the COUNT function but this produces
a syntax error. The method I expect to work is (simplified):
Select COUNT(DISTINCT PON)...
My actual current SQL is:
SELECT First([Order Setup Defects Query].[Defect Description]) AS [Defect
Description Field], Count([Order Setup Defects Query].[Defect Description])
AS DefectCounts, [Order Setup Defects Query].Responsibility
FROM [Order Setup Defects Query]
WHERE ((([Order Setup Defects Query].[Order Date]) Between
[Forms]![Main]![FromDate_combo] And [Forms]![Main]![ToDate_combo]) AND
(([Forms]![Main]![Product_combo])=[Order Setup Defects Query].[Product Name]
Or ([Forms]![Main]![Product_combo]) Is Null) AND
(([Forms]![Main]![Responsibility_combo])=[Order Setup Defects
Query].[Responsibility] Or ([Forms]![Main]![Responsibility_combo]) Is Null))
OR ((([Forms]![Main]![Product_combo])=[Order Setup Defects Query].[Product
Name] Or ([Forms]![Main]![Product_combo]) Is Null) AND
(([Forms]![Main]![Responsibility_combo])=[Order Setup Defects
Query].[Responsibility] Or ([Forms]![Main]![Responsibility_combo]) Is Null)
AND (([Forms]![Main]![FromDate_combo]) Is Null) AND
(([Forms]![Main]![ToDate_combo]) Is Null))
GROUP BY [Order Setup Defects Query].Responsibility, [Order Setup Defects
Query].[Defect ID], [Order Setup Defects Query].[Defect Description]
HAVING (((Count([Order Setup Defects Query].[Defect Description]))>=1))
ORDER BY Count([Order Setup Defects Query].[Defect Description]) DESC ,
[Order Setup Defects Query].Responsibility DESC , [Order Setup Defects
Query].[Defect ID];
Anyone have any idea how I can produce the unique purchase order count?
Thanks,
Randall Arnold
report has one Purchase Order Number (PON) but may have many errors
associated with that order. I aggregate errors based on Description and get
totals of each type. Date ranges are automatically filtered based on dates
on a user form. I can also filter based on Product Type and Area of
Responsibility off of this same user form. This part of my query works as
expected. I have a chart on a report that utilizes this query and it also
functions as desired.
However, I also need to determine the total number of unique purchase orders
and have that number show up in the query rows. I need this because I want
to be able to show, row by row, how many defects occurred out of how many
unique purchase orders (ie, opportunities for error).
I have tried numerous ways to get this to work and all have failed.
Microsoft Access does not seem to follow SQL convention 100% in that I should
be able to use a DISTINCT aggregate with the COUNT function but this produces
a syntax error. The method I expect to work is (simplified):
Select COUNT(DISTINCT PON)...
My actual current SQL is:
SELECT First([Order Setup Defects Query].[Defect Description]) AS [Defect
Description Field], Count([Order Setup Defects Query].[Defect Description])
AS DefectCounts, [Order Setup Defects Query].Responsibility
FROM [Order Setup Defects Query]
WHERE ((([Order Setup Defects Query].[Order Date]) Between
[Forms]![Main]![FromDate_combo] And [Forms]![Main]![ToDate_combo]) AND
(([Forms]![Main]![Product_combo])=[Order Setup Defects Query].[Product Name]
Or ([Forms]![Main]![Product_combo]) Is Null) AND
(([Forms]![Main]![Responsibility_combo])=[Order Setup Defects
Query].[Responsibility] Or ([Forms]![Main]![Responsibility_combo]) Is Null))
OR ((([Forms]![Main]![Product_combo])=[Order Setup Defects Query].[Product
Name] Or ([Forms]![Main]![Product_combo]) Is Null) AND
(([Forms]![Main]![Responsibility_combo])=[Order Setup Defects
Query].[Responsibility] Or ([Forms]![Main]![Responsibility_combo]) Is Null)
AND (([Forms]![Main]![FromDate_combo]) Is Null) AND
(([Forms]![Main]![ToDate_combo]) Is Null))
GROUP BY [Order Setup Defects Query].Responsibility, [Order Setup Defects
Query].[Defect ID], [Order Setup Defects Query].[Defect Description]
HAVING (((Count([Order Setup Defects Query].[Defect Description]))>=1))
ORDER BY Count([Order Setup Defects Query].[Defect Description]) DESC ,
[Order Setup Defects Query].Responsibility DESC , [Order Setup Defects
Query].[Defect ID];
Anyone have any idea how I can produce the unique purchase order count?
Thanks,
Randall Arnold