By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
| sqlite_version() |
|---|
| 3.39.1 |
✓
✓
✓
✓
✓
| book_key | book |
|---|---|
| 1 | A |
| 2 | B |
✓
| purchase_key | date_key | book_key | cost |
|---|---|---|---|
| 1 | 20231101 | 1 | 900 |
| 2 | 20231201 | 2 | 750 |
✓
| order_key | date_key | book_key | price |
|---|---|---|---|
| 1 | 20240101 | 1 | 1080 |
| 2 | 20240101 | 2 | 900 |
✓
| date_key | period_key |
|---|---|
| 20231101 | 202311 |
| 20231201 | 202312 |
| 20240101 | 202401 |
✓
| date_key | book | buys | sells | cum_buys | cum_sells | delta |
|---|---|---|---|---|---|---|
| 20231101 | A | 1 | null | 1 | null | 1 |
| 20231201 | A | null | null | 1 | null | 1 |
| 20240101 | A | null | 1 | 1 | 1 | 0 |
| 20231101 | B | null | null | null | null | 0 |
| 20231201 | B | 1 | null | 1 | null | 1 |
| 20240101 | B | null | 1 | 1 | 1 | 0 |
| period_key | book | max(t1.delta) |
|---|---|---|
| 202311 | A | 1 |
| 202311 | B | 0 |
| 202312 | A | 1 |
| 202312 | B | 1 |
| 202401 | A | 0 |
| 202401 | B | 0 |