I have 4 tables:
- Customer (CustomerID(PK), FirstName, LastName)
- Order (OrderID(PK), CustomerID(FK))
- Purchase (OrderID(FK), GameID(FK), Quantity)
- Games (GameID(PK), RetailPrice)
I would like to list the names of all customers whose orders total more than $150.
What I had that works:
SELECT
"p"."OrderID", SUM("p"."Quantity" * "g"."RetailPrice") "TotalPrice"
FROM
"Purchase" "p"
JOIN
"Games" "g" ON ("p"."GameID" = "g"."GameID")
GROUP BY
"p"."OrderID"
HAVING
SUM("p"."Quantity" * "g"."RetailPrice") >= 150
I tried joining the customer and order tables, which works, but once I try to select the firstname and lastname columns, it does not work; I get an error
Not in aggregate function or group by clause
SELECT
"c"."FirstName", "c"."LastName",
"p"."OrderID", SUM("p"."Quantity" * "g"."RetailPrice") "TotalPrice"
FROM
"Customer" "c"
JOIN
"Order" "o" ON "c"."CustomerID" = "o"."CustomerID"
JOIN
"Purchase" "p" ON "o"."OrderID" = "p"."OrderID"
JOIN
"Games" "g" ON ("p"."GameID" = "g"."GameID")
GROUP BY
"p"."OrderID"
HAVING
SUM("p"."Quantity" * "g"."RetailPrice") >= 150
None of the explanations I've seen online relating to aggregate functions and joining tables have been sufficient to make me understand what I need to do to make this work. What is the correct query for this and why is that the correct query?