I'm trying to use a view statement within a select statement, but at the same time I'm attempting to include an aggregate function which includes joins inside of the VIEW table.
What I'm not understanding is whether or not I need to rejoin the other tables. Why doesn't this work while using SalesData as a table?
The error I get is:
"SQL Error (1054): Unknown column 'SalesData.unit_price' in 'field list'
With this, I believe the error actually occurs here:
SUM(ROUND(SalesData.unit_price * SalesData.order_qty,2)) as 'Total Spent'
CREATE VIEW SalesData AS
SELECT orders.customer_id,
SUM(ROUND(items.unit_price * order_details.order_qty,2)) as 'Total Spent'
FROM order_details
JOIN orders on orders.order_id = order_details.order_id
JOIN items on items.item_id = order_details.item_id
GROUP BY orders.customer_id;
SELECT CONCAT(customers.customer_first_name, ' ', customers.customer_last_name) as 'Customer',
SUM(ROUND(SalesData.unit_price * SalesData.order_qty,2)) as 'Total Spent'
FROM customers
JOIN SalesData on SalesData.customer_id = customers.customer_id
GROUP BY customer_id;
unit_pricequalified to theSalesDataview - you only useitems.unit_price. It's really that SQL statement causing the error?SalesDataview only exposes two columns in itsSELECTlist --customier_id, Total Spent. Should it also be includingunit_price, order_quantitycolumn that you intend to use in the join, or should those be derived from an additional join toordersin your final query?