Question

I am using Microsoft Access and am having difficulty with this scenario for two reasons:

  1. the ORDER BY function is not working
  2. When I run the query, a pop up box appears asking for a value to be entered for SalesOrder.PID. When I enter a 1, the datasheet appears.

Any advice would be much appreciated regarding my code below:

SELECT 
   Product.PID, 
   Product.Code,
   COUNT (SalesOrder.PID) AS Ordered,
   SUM (ExtendedPrice) AS [Value], 
   SUM (NbrItemsRequested) AS Requested
FROM Product, SalesOrderProduct
WHERE 
   Product.PID = SalesOrderProduct.PID
GROUP BY 
   Product.PID, Product.Code
ORDER BY 
  Requested;
Was it helpful?

Solution

The pop-up box appears because SalesOrder.PID references a table SalesOrder that is not in the from clause. Presumably you mean SalesOrderProduct.

You should also use explicit joins in your query, and table aliases for all columns:

SELECT p.PID, p.Code, COUNT(sop.PID) AS Ordered,
       SUM(sop.ExtendedPrice) AS [Value], 
       SUM(sop.NbrItemsRequested) AS Requested
FROM Product as p inner join
     SalesOrderProduct as sop
     on p.PID = sop.PID
GROUP BY p.PID, p.Code
ORDER BY SUM(sop.NbrItemsRequested);

I'm not sure what you mean by the order by not working, but I'm guessing that putting the formula in instead of the alias will fix the problem.

Note: the above is guessing as to where ExtendedPrice and NbrItemsRequested come from.

OTHER TIPS

If a pop up box appears asking for the SalesOrder.PID value this means Access doesn't recognise that column anywhere in the tables you've included in the query. In other words you are not counting SalesOrder.PID as Access doesn't recognise this. I would recommend checking your column names to make sure they are correct.

This should fix it.

SELECT Product.PID, Product.Code,

COUNT (SalesOrderProduct.PID) AS Ordered,

SUM (ExtendedPrice) AS [Value], 
SUM (NbrItemsRequested) AS Requested

FROM Product inner join  SalesOrderProduct on Product.PID = SalesOrderProduct.PID

GROUP BY Product.PID, Product.Code

ORDER BY Requested;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top