Creating a Customer Order Summary View
You are tasked with creating a reusable data abstraction for your company's customer service team. This team frequently needs to see a summary of each customer's total order value and the number of orders they have placed. Creating a view will allow them to query this summarized data easily without needing to write complex JOIN and GROUP BY clauses every time.
Problem Description
Your goal is to create a SQL VIEW named CustomerOrderSummary. This view should display the following information for each customer:
- CustomerID: The unique identifier for the customer.
- TotalOrderValue: The sum of the
Amountfrom all orders placed by that customer. - NumberOfOrders: The total count of orders placed by that customer.
The view should be based on two tables:
Customers: Contains customer information, includingCustomerIDandCustomerName.Orders: Contains order details, includingOrderID,CustomerID, andAmount.
Key Requirements:
- Use the
CREATE VIEWSQL statement. - The view should join the
CustomersandOrderstables onCustomerID. - The view should group the results by
CustomerIDto aggregate order information per customer. - The view should calculate the sum of
Amountfor each customer, aliased asTotalOrderValue. - The view should count the number of orders for each customer, aliased as
NumberOfOrders. - The view should include the
CustomerIDin the output.
Expected Behavior:
When a SELECT * FROM CustomerOrderSummary query is executed, it should return a result set containing CustomerID, TotalOrderValue, and NumberOfOrders for every customer who has placed at least one order. Customers with no orders should not appear in the view.
Edge Cases to Consider:
- Customers who have not placed any orders should not be included in the
CustomerOrderSummaryview.
Examples
Example 1:
Input Tables:
Customers Table:
| CustomerID | CustomerName |
|---|---|
| 101 | Alice Smith |
| 102 | Bob Johnson |
| 103 | Charlie Brown |
Orders Table:
| OrderID | CustomerID | Amount |
|---|---|---|
| 1 | 101 | 150.00 |
| 2 | 102 | 200.50 |
| 3 | 101 | 75.25 |
| 4 | 103 | 50.00 |
| 5 | 101 | 100.00 |
Output View (if queried with SELECT * FROM CustomerOrderSummary):
| CustomerID | TotalOrderValue | NumberOfOrders |
|---|---|---|
| 101 | 325.25 | 3 |
| 102 | 200.50 | 1 |
| 103 | 50.00 | 1 |
Explanation:
- Customer 101 has 3 orders with amounts 150.00, 75.25, and 100.00, summing to 325.25.
- Customer 102 has 1 order with an amount of 200.50.
- Customer 103 has 1 order with an amount of 50.00.
- Customer 103, who exists in the
Customerstable but has no entries in theOrderstable, is not included in the output.
Example 2:
Input Tables:
Customers Table:
| CustomerID | CustomerName |
|---|---|
| 201 | David Lee |
| 202 | Eve Adams |
Orders Table:
| OrderID | CustomerID | Amount |
|---|---|---|
| 6 | 201 | 300.00 |
Output View (if queried with SELECT * FROM CustomerOrderSummary):
| CustomerID | TotalOrderValue | NumberOfOrders |
|---|---|---|
| 201 | 300.00 | 1 |
Explanation:
- Customer 201 has one order totaling 300.00.
- Customer 202 has no orders and therefore is not present in the summary.
Constraints
- The
Customerstable contains at least 1 customer record. - The
Orderstable may be empty or contain up to 10,000 order records. - The
Amountin theOrderstable will be a non-negative numeric value. CustomerIDwill be a unique identifier in theCustomerstable and will be used as a foreign key in theOrderstable.- The
CREATE VIEWstatement must be valid SQL.
Notes
- Consider the appropriate SQL aggregate functions for summing amounts and counting records.
- Ensure your
JOINcondition correctly links customers to their orders. - The
GROUP BYclause is crucial for aggregating results per customer. - The problem statement implicitly requires an
INNER JOINor equivalent logic to exclude customers without orders from the view.