Using the orders table, calculate the average order amount of the current, 1 preceding, and 1 following orders for each user. Do the calculation based on the order_id in ascending order.
Let's go through an example:
| buyer_id | order_id | order_amount |
|---|---|---|
| 101 | 201 | 120.00 |
| 101 | 202 | 75.00 |
| 101 | 207 | 140.00 |
| 101 |
| 238 |
| 350.00 |
| 101 | 243 | 95.00 |
buyer_id 101 had 5 orders, for each row, you want to calculate the average order_amount of the row prior until next row. If we look at the second row, user_id, 101 spent $75.
We want to average the value prior (140) and the current row ($75). And do that across each row of the orders table.
orders table:
| Column Name | Description |
|---|---|
| order_id | Unique identifier for each order. |
| buyer_id | The user_id of the user who placed the order. |
| seller_id | The user_id of the user who is selling the item. |
| order_amount | The total amount of the order in currency units. |
| buyer_id | average_amount |
|---|---|
| 101 | 111.666 |
| 101 | 171.25 |
| 101 | 124.3 |
| 102 | 112.1 |
Using the orders table, calculate the average order amount of the current, 1 preceding, and 1 following orders for each user. Do the calculation based on the order_id in ascending order.
Let's go through an example:
| buyer_id | order_id | order_amount |
|---|---|---|
| 101 | 201 | 120.00 |
| 101 | 202 | 75.00 |
| 101 | 207 | 140.00 |
| 101 | 238 | 350.00 |
| 101 | 243 | 95.00 |
buyer_id 101 had 5 orders, for each row, you want to calculate the average order_amount of the row prior until next row. If we look at the second row, user_id, 101 spent $75.
We want to average the value prior (140) and the current row ($75). And do that across each row of the orders table.
orders table:
| Column Name | Description |
|---|---|
| order_id | Unique identifier for each order. |
| buyer_id | The user_id of the user who placed the order. |
| seller_id | The user_id of the user who is selling the item. |
| order_amount | The total amount of the order in currency units. |
| buyer_id | average_amount |
|---|---|
| 101 | 111.666 |
| 101 | 171.25 |
| 101 | 124.3 |
| 102 | 112.1 |