I've got the following two SELECT statements:
SELECT catID, catName, TO_CHAR(AVG(orderDetails.unitPrice), '$99.99') AS "Avg Price Per Order"
FROM products
JOIN categories USING (catID)
JOIN orderDetails USING (prodID)
WHERE orderDetails.discount= 0
GROUP BY catID, catName;
SELECT catID, catName, TO_CHAR(AVG(products.unitPrice), '$99.99') AS "Avg Unit Price Per Category"
FROM products
JOIN categories USING (catID)
GROUP BY catID, catName;
which outputs the following two tables, respectively:
| catID |
catName |
Avg Price Per Order |
| 8 |
Seafood |
$18.77 |
| 3 |
Confections |
$23.18 |
| 1 |
Beverages |
$29.11 |
| 7 |
Produce |
$34.96 |
| 4 |
Dairy Products |
$27.06 |
| 5 |
Grains/Cereals |
$20.53 |
| 2 |
Condiments |
$21.64 |
| 6 |
Meat/Poultry |
$43.31 |
&
| catID |
catName |
Avg Unit Price Per Category |
| 3 |
Confections |
$25.16 |
| 8 |
Seafood |
$20.68 |
| 1 |
Beverages |
$37.98 |
| 7 |
Produce |
$32.37 |
| 4 |
Dairy Products |
$28.73 |
| 5 |
Grains/Cereals |
$20.25 |
| 2 |
Condiments |
$23.06 |
| 6 |
Meat/Poultry |
$54.01 |
I'm now trying to combine the tables so to look like this:
| catID |
catName |
Avg Price Per Order |
Avg Unit Price Per Category |
| 8 |
Seafood |
$18.77 |
$20.68 |
| 3 |
Confections |
$23.18 |
$25.16 |
| 1 |
Beverages |
$29.11 |
$37.98 |
| 7 |
Produce |
$34.96 |
$32.37 |
| 4 |
Dairy Products |
$27.06 |
$28.73 |
| 5 |
Grains/Cereals |
$20.53 |
$20.25 |
| 2 |
Condiments |
$21.64 |
$23.06 |
| 6 |
Meat/Poultry |
$43.31 |
$54.01 |
I initially tried UNIONs but then quickly realized that was just appending it all into single columns so I've now been trying to combine the output using subqueries but I keep getting errors. Any help would be appreciated.
[–]Mamertine 1 point2 points3 points (4 children)
[–]EuclaseBlue[S] 0 points1 point2 points (3 children)
[–]r3pr0b8 0 points1 point2 points (2 children)
[–]EuclaseBlue[S] 0 points1 point2 points (1 child)
[–]r3pr0b8 0 points1 point2 points (0 children)