LAST_VALUE: Show Last Day's Sales on Every Row

SQL coding challenge · Difficulty: easy · +50 XP

Problem

The daily summary footer needs the final day's sales amount on every row — so any row can be used as a reference for the period's closing figure.

Tables

Table: daily_sales

| sale_id | day_name | amount |
| 1 | Mon | 100 |
| 2 | Tue | 200 |
| 3 | Wed | 300 |
| 4 | Thu | 400 |
| 5 | Fri | 500 |

Expected Output

| sale_id | day_name | amount | last_day_sales |
| 1 | Mon | 100 | 500 |
| 2 | Tue | 200 | 500 |
| 3 | Wed | 300 | 500 |
| 4 | Thu | 400 | 500 |
| 5 | Fri | 500 | 500 |
  • Return: sale_id, day_name, amount, last_day_sales
  • last_day_sales = the amount from the last row (same value on every row)
  • Sort by sale_id ascending
  • Important: LAST_VALUE requires ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING in the frame clause

Solve this challenge on PySpark.in