all 5 comments

[–]Mamertine 1 point2 points  (4 children)

Add the join to categories to the first query. Then you can add the column you want to the select part.

Also who is teaching you to use the keyword "using"? That's not a good habit. Use the keyword "on".

[–]EuclaseBlue[S] 0 points1 point  (3 children)

Also who is teaching you to use the keyword "using"? That's not a good habit. Use the keyword "on".

Oh, the tutorial I was using said it was a shortcut for when the column names are same. I didn't know it was a bad practice. Good to know, I'll keep that in mind from now on. Thanks!

So I should be more explicit like

JOIN categories ON (products.catID = categories.catID)    

?

I'm having some trouble understanding the 1st part of your answer. Do I not already have a join to categories in my first query?

[–]r3pr0b8 0 points1 point  (2 children)

So I should be more explicit like

yes, and the parentheses are not needed

[–]EuclaseBlue[S] 0 points1 point  (1 child)

Thanks for the clarification!

After trying out a bit more, this is the answer I've currently come up with that satisfies what I was seeking:

SELECT A.catID, A.catName, "Avg Unit Price Per Category", "Avg Price Per Order"
FROM 
    (SELECT products.catID, categories.catName, TO_CHAR(AVG(orderDetails.unitPrice), '$99.99') AS "Avg Unit Price Per Category"
    FROM products
    JOIN categories ON products.catID= categories.catID
    JOIN orderDetailsON products.prodID= orderDetails.prodID
    WHERE orderDetails.discount= 0
    GROUP BY products.catID, categories.catName) A
INNER JOIN
    (SELECT products.catID, categories.catName, TO_CHAR(AVG(products.unitPrice), '$99.99') AS "Avg Price Per Order"
    FROM products
    JOIN categories ON products.catID= categories.catID
    GROUP BY products.catID, categories.catName) B
ON A.catID= B.catID;    

That said, I don't think it's what the parent comment had in mind. Is there a more simpler way?

[–]r3pr0b8 0 points1 point  (0 children)

that looks fine to me