Skip to content
Advertisement

SQL Anywhere error – Function or column reference to ‘Fraction’ must also appear in a GROUP BY

I have this simple SQL SELECT that summarizes by ProductID but I get this “must appear” error.

SELECT Products.ProductID, 
   Products.Qtty / Catalog.Fraction as Amount
FROM   Products, Catalog
WHERE  Catalog.ID = Products.ID
GROUP BY Products.ProductID

SQL requires me to put column Fraction into GROUP BY clause which I do not need. I just need to summarize by ProductID. How do I build correct SQL statement here?

Thanks.

Advertisement

Answer

You need to use aggregate function:

SELECT Products.ProductID, 
   SUM(Products.Qtty) / SUM(Catalog.Fraction) as Amount
FROM Products
JOIN Catalog
  ON Catalog.ID = Products.ID
GROUP BY Products.ID
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement