[EDIT] I was told that the query has a Sub Query in it, So here is what i've come up with and what to work on now.[NEW CODE]
SELECT P.prodid, P.prodname, SUM(O.qtysold) AS "TOTAL SALES"
FROM ORDERLINE O
INNER JOIN PRODUCT P
ON O.prodid = P.prodid
INNER JOIN ALLOCATION A
ON P.prodid = A.prodid
GROUP BY P.prodid, P.prodname
HAVING (SELECT AVG(score) AS score
FROM QUALITYCHECK
HAVING score >= 2)
ORDER BY 3 ASC;
[ORIGINAL POST] So I've been trying to do different ways of this code to give me the right results. I think this is my most accurate one if it works:
SELECT P.prodid, P.prodname, SUM(O.qtysold) AS "TOTAL SALES"
FROM ORDERLINE O
INNER JOIN PRODUCT P
ON O.prodid = P.prodid
INNER JOIN ALLOCATION A
ON P.prodid = A.prodid
GROUP BY P.prodid, P.prodname
WHERE (SELECT AVG(score) FROM QUALITYCHECK) >= 2
ORDER BY 3 ASC;
~Error is line 8~
So let me explain what I'm trying to accomplish. I'm trying to grab the quantity of sold products from the Orderline table but only where the quality check score average of the product is greater than 2. I would like to try and make this work, but if there is another way of doing it, all suggestions are welcome.
I have another way of doing it also, but it's giving me results of 3 and 15 for the SUM instead of 1 and 5, I supose I will also put that here incase anyone can fix that.
SELECT P.prodid, P.prodname, SUM(O.qtysold) AS "TOTAL SALES"
FROM ORDERLINE O
INNER JOIN PRODUCT P
ON O.prodid = P.prodid
INNER JOIN ALLOCATION A
ON P.prodid = A.prodid
INNER JOIN QUALITYCHECK Q
ON A.prodid = Q.prodid
GROUP BY P.prodid, P.prodname
HAVING AVG(Q.score) >= 2
ORDER BY 3 ASC;
Thanks in Advance :D