Count the days whose volume is strictly higher than the previous day's (use LAG()). The first day is never counted. Return a single row with one column GrowthDays.
Input Table(s)
Table: Sales
| Column | Type | Meaning |
|---|
| sale_date | DATE | Day the volume is recorded for. Primary key. |
| volume | INT | Sales volume on that day. NOT NULL. |
Keys: sale_date is the primary key, so there is one row per date.
Output Contract
-
Exactly one row with exactly one column, named
GrowthDays
with that capitalization.
-
GrowthDays
is the number of rows whose
volume
is strictly greater than the
volume
of the previous row in
sale_date
order.
-
If no day grows, the row still appears with
GrowthDays = 0
.
Example
Sales
| sale_date | volume |
|---|
| 2024-01-01 | 100 |
| 2024-01-02 | 150 |
| 2024-01-03 | 120 |
| 2024-01-04 | 200 |
| 2024-01-05 | 200 |
| 2024-01-06 | 250 |
Expected result
Day 2 (150 > 100), day 4 (200 > 120) and day 6 (250 > 200) have higher volume than their previous day. Day 3 decreased and day 5 stayed the same, so GrowthDays is 3.
Constraints and Clarifications
-
The table has 1 to 10,000 rows, one per
sale_date
.
-
volume
is a non-negative integer and is never NULL.
-
The previous day means the previous row when the table is ordered by
sale_date
, which is what
LAG()
returns. If a date is missing, for example
2024-01-03
, then
2024-01-04
is compared with
2024-01-02
.
-
The earliest date has no previous day and is never counted. A table with a single row returns 0.
-
Equal volume on consecutive days is not growth.