Write a SELECT statement that uses aggregate window functions to calculate the order total for each customer and the order total for each customer by date. Return these columns: The customer_id column from the Orders table The order_date column from the Orders table The total amount for each order item in the Order_Items table The sum of the order totals for each customer The sum of the order totals for each customer by date (Hint: You can create a peer group to get these values)

Respuesta :

Answer:

1. SELECT Order.customer_id, SUM(order_total) AS 'Total_order'  

FROM Order JOIN Order_item

WHERE Order.customer_id = Order_item.customer_id

GROUP BY Order.customer_id

2. SELECT Order.customer_id, SUM(order_total) AS 'Total_order' , order_date

FROM Order JOIN Order_item

WHERE Order.customer_id = Order_item.customer_id

GROUP BY Order.customer_id, Order_item.order_date

ORDER BY Order_item.order_date

Explanation:

Both SQL queries return the total order from the joint order and order_item tables, but the second query returns the total order and their data grouped by customer and order date and also ordered by the order date.