AGGREGATES WITH WINDOWS

Structured query language provides users the ability to compute aggregate values based on sequences of rows and the values the rows contain. A sequence of rows is commonly referred to as the window. The rows belonging to each window and the ordering of rows within each window are based on user-specified instructions. The significance of being able to combine window and aggregate functions is it allows users to easily calculate period-based measures such as running totals and moving averages. Additionally, if desired, the combination allows aggregate values to be computed without changing the level of detail or granularity of the data. Let us explore a few of real-world problems and their corresponding solutions to highlight the power of this feature.

Cupcake Shop
Suppose you have recently joined an analytics team at a world-renowned cupcake shop with several locations across the globe. The company recently opened up a new location and management has asked you to provide answers or a report addressing the following:

– On which date did the new location sell its 10,000th cupcake?

– Can a report be provided containing 7-day moving averages for each of our cupcake flavors in terms of quantity sold? The report should contain the same level of detail as how the data is currently stored (i.e., one row per report date and flavor along with the corresponding quantity sold). An attribute containing the 7-day moving average values should be included.

– Can a daily cupcake flavor performance report be provided containing a comparison between each day’s cupcake sales and the all-time high in terms of number of cupcakes sold per flavor? The report should contain the report date, flavor, quantity sold, the maximum quantity ever sold for the flavor, and the difference between those two values.

The data you are working with can be found in the table below. This fictional data set can be generated using the code found in the Data Set Generation Code section. To review the code, click on the icon to the left of the section’s header. To hide the code, click the icon again. To follow along and practice using this data set, you can copy and paste this code into livesql.oracle.com or your preferred query editor. Keep in mind, if you are not using an Oracle product, then slight changes to the code may be required for it to run.

report_dateflavorquantity_sold
08-NOV-23Chocolate10
08-NOV-23Lemon69
08-NOV-23Vanilla55
08-NOV-23Red Velvet49
09-NOV-23Chocolate18
09-NOV-23Red Velvet68
09-NOV-23Lemon45
09-NOV-23Vanilla83
10-NOV-23Red Velvet34
10-NOV-23Vanilla97
10-NOV-23Chocolate95
10-NOV-23Lemon52
11-NOV-23Chocolate7
11-NOV-23Vanilla61
11-NOV-23Lemon14
11-NOV-23Red Velvet39
12-NOV-23Chocolate58
12-NOV-23Lemon90
12-NOV-23Vanilla12
12-NOV-23Red Velvet92
13-NOV-23Chocolate52
13-NOV-23Red Velvet62
13-NOV-23Lemon84
13-NOV-23Vanilla50
14-NOV-23Red Velvet87
14-NOV-23Chocolate85
14-NOV-23Vanilla52
14-NOV-23Lemon67
15-NOV-23Chocolate52
15-NOV-23Vanilla80
15-NOV-23Red Velvet86
15-NOV-23Lemon28
16-NOV-23Vanilla44
16-NOV-23Red Velvet9
16-NOV-23Lemon78
16-NOV-23Chocolate1
17-NOV-23Chocolate94
17-NOV-23Red Velvet4
17-NOV-23Lemon97
17-NOV-23Vanilla63
18-NOV-23Red Velvet44
18-NOV-23Vanilla14
18-NOV-23Chocolate74
18-NOV-23Lemon21
19-NOV-23Chocolate52
19-NOV-23Red Velvet8
19-NOV-23Lemon7
19-NOV-23Vanilla78
20-NOV-23Chocolate53
20-NOV-23Lemon90
20-NOV-23Vanilla69
20-NOV-23Red Velvet35
21-NOV-23Chocolate17
21-NOV-23Red Velvet14
21-NOV-23Lemon77
21-NOV-23Vanilla47
22-NOV-23Red Velvet45
22-NOV-23Vanilla67
22-NOV-23Chocolate44
22-NOV-23Lemon71
23-NOV-23Chocolate58
23-NOV-23Vanilla55
23-NOV-23Lemon71
23-NOV-23Red Velvet78
24-NOV-23Vanilla81
24-NOV-23Red Velvet53
24-NOV-23Lemon12
24-NOV-23Chocolate41
25-NOV-23Chocolate38
25-NOV-23Red Velvet53
25-NOV-23Lemon32
25-NOV-23Vanilla30
26-NOV-23Red Velvet77
26-NOV-23Vanilla89
26-NOV-23Chocolate30
26-NOV-23Lemon1
27-NOV-23Chocolate50
27-NOV-23Red Velvet35
27-NOV-23Lemon86
27-NOV-23Vanilla5
28-NOV-23Chocolate85
28-NOV-23Lemon16
28-NOV-23Vanilla17
28-NOV-23Red Velvet6
29-NOV-23Chocolate20
29-NOV-23Red Velvet2
29-NOV-23Lemon14
29-NOV-23Vanilla97
30-NOV-23Red Velvet92
30-NOV-23Chocolate55
30-NOV-23Vanilla20
30-NOV-23Lemon97
01-DEC-23Red Velvet10
01-DEC-23Lemon73
01-DEC-23Vanilla64
01-DEC-23Chocolate84
02-DEC-23Chocolate77
02-DEC-23Red Velvet38
02-DEC-23Lemon43
02-DEC-23Vanilla85
03-DEC-23Chocolate39
03-DEC-23Vanilla46
03-DEC-23Red Velvet42
03-DEC-23Lemon23
04-DEC-23Red Velvet63
04-DEC-23Vanilla35
04-DEC-23Chocolate92
04-DEC-23Lemon19
05-DEC-23Red Velvet45
05-DEC-23Vanilla20
05-DEC-23Chocolate51
05-DEC-23Lemon0
06-DEC-23Lemon48
06-DEC-23Red Velvet76
06-DEC-23Vanilla23
06-DEC-23Chocolate16
07-DEC-23Chocolate29
07-DEC-23Vanilla95
07-DEC-23Red Velvet45
07-DEC-23Lemon5
08-DEC-23Vanilla57
08-DEC-23Red Velvet43
08-DEC-23Lemon10
08-DEC-23Chocolate55
09-DEC-23Chocolate63
09-DEC-23Red Velvet88
09-DEC-23Lemon29
09-DEC-23Vanilla92
10-DEC-23Red Velvet26
10-DEC-23Vanilla96
10-DEC-23Chocolate94
10-DEC-23Lemon57
11-DEC-23Chocolate65
11-DEC-23Red Velvet61
11-DEC-23Lemon35
11-DEC-23Vanilla29
12-DEC-23Chocolate99
12-DEC-23Lemon58
12-DEC-23Vanilla62
12-DEC-23Red Velvet42
13-DEC-23Chocolate68
13-DEC-23Red Velvet52
13-DEC-23Lemon97
13-DEC-23Vanilla25
14-DEC-23Red Velvet78
14-DEC-23Vanilla99
14-DEC-23Chocolate17
14-DEC-23Lemon81
15-DEC-23Chocolate80
15-DEC-23Vanilla92
15-DEC-23Lemon81
15-DEC-23Red Velvet76
16-DEC-23Vanilla82
16-DEC-23Red Velvet59
16-DEC-23Lemon82
16-DEC-23Chocolate29
17-DEC-23Chocolate55
17-DEC-23Red Velvet86
17-DEC-23Lemon7
17-DEC-23Vanilla10
18-DEC-23Red Velvet93
18-DEC-23Vanilla30
18-DEC-23Chocolate2
18-DEC-23Lemon4
19-DEC-23Chocolate81
19-DEC-23Red Velvet69
19-DEC-23Lemon48
19-DEC-23Vanilla25
20-DEC-23Chocolate88
20-DEC-23Lemon95
20-DEC-23Vanilla17
20-DEC-23Red Velvet45
21-DEC-23Chocolate30
21-DEC-23Red Velvet18
21-DEC-23Lemon64
21-DEC-23Vanilla37
22-DEC-23Red Velvet98
22-DEC-23Chocolate100
22-DEC-23Vanilla73
22-DEC-23Lemon61
23-DEC-23Chocolate1
23-DEC-23Vanilla52
23-DEC-23Red Velvet5
23-DEC-23Lemon81
24-DEC-23Red Velvet2
24-DEC-23Vanilla63
24-DEC-23Chocolate33
24-DEC-23Lemon21
25-DEC-23Lemon12
25-DEC-23Red Velvet61
25-DEC-23Vanilla40
25-DEC-23Chocolate78
26-DEC-23Chocolate9
26-DEC-23Vanilla81
26-DEC-23Red Velvet16
26-DEC-23Lemon48
27-DEC-23Chocolate51
27-DEC-23Vanilla30
27-DEC-23Red Velvet7
27-DEC-23Lemon89
28-DEC-23Red Velvet0
28-DEC-23Vanilla44
28-DEC-23Chocolate81
28-DEC-23Lemon16
29-DEC-23Chocolate80
29-DEC-23Lemon95
29-DEC-23Vanilla28
29-DEC-23Red Velvet87
30-DEC-23Chocolate74
30-DEC-23Red Velvet80
30-DEC-23Lemon37
30-DEC-23Vanilla67
31-DEC-23Red Velvet49
31-DEC-23Vanilla14
31-DEC-23Chocolate27
31-DEC-23Lemon74
01-JAN-24Red Velvet13
01-JAN-24Vanilla21
01-JAN-24Chocolate38
01-JAN-24Lemon45
02-JAN-24Lemon81
02-JAN-24Red Velvet18
02-JAN-24Vanilla50
02-JAN-24Chocolate43
03-JAN-24Chocolate83
03-JAN-24Vanilla43
03-JAN-24Lemon28
03-JAN-24Red Velvet16
04-JAN-24Red Velvet51
04-JAN-24Vanilla1
04-JAN-24Chocolate82
04-JAN-24Lemon23
05-JAN-24Lemon61
05-JAN-24Red Velvet94
05-JAN-24Chocolate6
05-JAN-24Vanilla33
06-JAN-24Red Velvet3
06-JAN-24Lemon32
06-JAN-24Vanilla88
06-JAN-24Chocolate95
07-JAN-24Red Velvet50
07-JAN-24Vanilla11
07-JAN-24Chocolate74
07-JAN-24Lemon57
08-JAN-24Red Velvet22
08-JAN-24Lemon81
08-JAN-24Vanilla27
08-JAN-24Chocolate5
09-JAN-24Red Velvet64
09-JAN-24Vanilla66
09-JAN-24Chocolate5
09-JAN-24Lemon89
10-JAN-24Red Velvet80
10-JAN-24Lemon84
10-JAN-24Vanilla50
10-JAN-24Chocolate35
11-JAN-24Chocolate89
11-JAN-24Vanilla47
11-JAN-24Red Velvet51
11-JAN-24Lemon40
12-JAN-24Lemon54
12-JAN-24Red Velvet24
12-JAN-24Vanilla93
12-JAN-24Chocolate43
13-JAN-24Lemon22
13-JAN-24Red Velvet17
13-JAN-24Chocolate100
13-JAN-24Vanilla62
14-JAN-24Lemon69
14-JAN-24Red Velvet93
14-JAN-24Vanilla100
14-JAN-24Chocolate40
15-JAN-24Lemon25
15-JAN-24Red Velvet57
15-JAN-24Chocolate75
15-JAN-24Vanilla72
16-JAN-24Lemon34
16-JAN-24Red Velvet70
16-JAN-24Vanilla94
16-JAN-24Chocolate94
17-JAN-24Chocolate35
17-JAN-24Vanilla35
17-JAN-24Red Velvet93
17-JAN-24Lemon77
18-JAN-24Red Velvet78
18-JAN-24Vanilla90
18-JAN-24Chocolate17
18-JAN-24Lemon97
19-JAN-24Red Velvet16
19-JAN-24Vanilla44
19-JAN-24Chocolate33
19-JAN-24Lemon71
20-JAN-24Red Velvet96
20-JAN-24Vanilla95
20-JAN-24Chocolate17
20-JAN-24Lemon35
21-JAN-24Red Velvet55
21-JAN-24Vanilla2
21-JAN-24Chocolate5
21-JAN-24Lemon24
22-JAN-24Red Velvet57
22-JAN-24Lemon88
22-JAN-24Vanilla11
22-JAN-24Chocolate96
23-JAN-24Red Velvet74
23-JAN-24Vanilla70
23-JAN-24Chocolate48
23-JAN-24Lemon88
24-JAN-24Red Velvet72
24-JAN-24Lemon84
24-JAN-24Vanilla45
24-JAN-24Chocolate23
25-JAN-24Lemon81
25-JAN-24Red Velvet31
25-JAN-24Vanilla44
25-JAN-24Chocolate21
26-JAN-24Red Velvet97
26-JAN-24Vanilla49
26-JAN-24Chocolate5
26-JAN-24Lemon90
27-JAN-24Lemon93
27-JAN-24Red Velvet7
27-JAN-24Chocolate63
27-JAN-24Vanilla97
28-JAN-24Lemon4
28-JAN-24Red Velvet42
28-JAN-24Chocolate29
28-JAN-24Vanilla28
29-JAN-24Lemon70
29-JAN-24Red Velvet98
29-JAN-24Vanilla5
29-JAN-24Chocolate87
30-JAN-24Lemon87
30-JAN-24Red Velvet21
30-JAN-24Chocolate54
30-JAN-24Vanilla48
31-JAN-24Lemon11
31-JAN-24Red Velvet4
31-JAN-24Vanilla92
31-JAN-24Chocolate76
01-FEB-24Red Velvet93
01-FEB-24Lemon88
01-FEB-24Vanilla68
01-FEB-24Chocolate65
02-FEB-24Chocolate72
02-FEB-24Red Velvet37
02-FEB-24Lemon24
02-FEB-24Vanilla36
03-FEB-24Chocolate96
03-FEB-24Vanilla89
03-FEB-24Red Velvet52
03-FEB-24Lemon91
04-FEB-24Red Velvet4
04-FEB-24Vanilla100
04-FEB-24Chocolate1
04-FEB-24Lemon86
05-FEB-24Red Velvet91
05-FEB-24Vanilla48
05-FEB-24Chocolate34
05-FEB-24Lemon71
06-FEB-24Lemon63
06-FEB-24Red Velvet65
06-FEB-24Vanilla57
06-FEB-24Chocolate7
Data Set Generation Code

Keep in mind, the data set you generate using the code below may contain different quantity sold values due to the use of the DBMS_RANDOM.VALUE function.

EXEC DBMS_RANDOM.SEED(42);

CREATE TABLE daily_report AS 
    WITH daily_report (report_date, flavor, quantity_sold) AS (
        SELECT
    		daily_sale.report_date,
    		data_template.flavor_value,
    		daily_sale.quantity_sold
    	FROM (
    		SELECT
    		   'Chocolate' AS flavor_1,
    		   'Vanilla' AS flavor_2,
    		   'Red Velvet' AS flavor_3,
    		   'Lemon' AS flavor_4
    		FROM
    			dual
    	)
    	UNPIVOT (
    		flavor_value FOR flavor_column IN (
    			"FLAVOR_1", "FLAVOR_2",
    			"FLAVOR_3", "FLAVOR_4")
    	) data_template
    	CROSS JOIN (
    	SELECT
    		TRUNC(SYSDATE) AS report_date,
    		FLOOR(DBMS_RANDOM.VALUE(0, 101)) AS quantity_sold
    	FROM
    		dual
    	) daily_sale
        UNION ALL
        SELECT
            daily_report.report_date - INTERVAL '1' DAY,
         	daily_report.flavor,
            FLOOR(DBMS_RANDOM.VALUE(0, 101))
        FROM
            daily_report
        WHERE
            daily_report.report_date > TRUNC(SYSDATE) - INTERVAL '90' DAY
    )
    SELECT
    	daily_report.report_date,
    	daily_report.flavor,
    	daily_report.quantity_sold
    FROM
    	daily_report;

Getting back to management’s questions, how can SQL be used to generate the answers? Let tackle each question in turn, starting with determining the date for which the 10,000th cupcake was sold.

On which date did the new location sell its 10,000th cupcake?
To determine the date for which the 10,000th cupcake was sold, the number of cupcakes sold per day requires computing. This can be achieved by using the aggregate function, SUM, with a GROUP BY clause specifying to group the rows by report date values. The query described so far can be found below and corresponding output can be found below.

SELECT
    daily_report.report_date,
    SUM(daily_report.quantity_sold) AS quantity_sold
FROM
    daily_report
GROUP BY
    daily_report.report_date;
report_datequantity_sold
08-NOV-23183
09-NOV-23214
10-NOV-23278
11-NOV-23121
12-NOV-23252
13-NOV-23248
14-NOV-23291
15-NOV-23246
16-NOV-23132
17-NOV-23258
18-NOV-23153
19-NOV-23145
20-NOV-23247
21-NOV-23155
22-NOV-23227
23-NOV-23262
24-NOV-23187
25-NOV-23153
26-NOV-23197
27-NOV-23176
28-NOV-23124
29-NOV-23133
30-NOV-23264
01-DEC-23231
02-DEC-23243
03-DEC-23150
04-DEC-23209
05-DEC-23116
06-DEC-23163
07-DEC-23174
08-DEC-23165
09-DEC-23272
10-DEC-23273
11-DEC-23190
12-DEC-23261
13-DEC-23242
14-DEC-23275
15-DEC-23329
16-DEC-23252
17-DEC-23158
18-DEC-23129
19-DEC-23223
20-DEC-23245
21-DEC-23149
22-DEC-23332
23-DEC-23139
24-DEC-23119
25-DEC-23191
26-DEC-23154
27-DEC-23177
28-DEC-23141
29-DEC-23290
30-DEC-23258
31-DEC-23164
01-JAN-24117
02-JAN-24192
03-JAN-24170
04-JAN-24157
05-JAN-24194
06-JAN-24218
07-JAN-24192
08-JAN-24135
09-JAN-24224
10-JAN-24249
11-JAN-24227
12-JAN-24214
13-JAN-24201
14-JAN-24302
15-JAN-24229
16-JAN-24292
17-JAN-24240
18-JAN-24282
19-JAN-24164
20-JAN-24243
21-JAN-2486
22-JAN-24252
23-JAN-24280
24-JAN-24224
25-JAN-24177
26-JAN-24241
27-JAN-24260
28-JAN-24103
29-JAN-24260
30-JAN-24210
31-JAN-24183
01-FEB-24314
02-FEB-24169
03-FEB-24328
04-FEB-24191
05-FEB-24244
06-FEB-24192

After computing the quantity sold value for each day the cupcake shop has been open, a running total needs to be computed across the report date values. To compute the running total, the summation of quantity sold values needs to be wrapped within another SUM and an OVER clause specifying one implicit partition (i.e., the entire data set) is needed. Within the OVER clause, a window clause stating to compute each day’s running daily total value based on the sum of the quantity sold values for all days (i.e., rows) prior to the current row’s day value and the current row’s day value is required. When computing the running total, it is important to order the report date values in ascending order within the OVER clause. This will ensure the running total starts with the first day the shop was open. For clarity, see the augmented query and corresponding result set below.

SELECT
    daily_report.report_date,
    SUM(SUM(daily_report.quantity_sold)) OVER (
        ORDER BY daily_report.report_date
        ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    ) AS running_daily_total
FROM
    daily_report
GROUP BY
    daily_report.report_date;
report_daterunning_daily_total
08-NOV-23183
09-NOV-23397
10-NOV-23675
11-NOV-23796
12-NOV-231048
13-NOV-231296
14-NOV-231587
15-NOV-231833
16-NOV-231965
17-NOV-232223
18-NOV-232376
19-NOV-232521
20-NOV-232768
21-NOV-232923
22-NOV-233150
23-NOV-233412
24-NOV-233599
25-NOV-233752
26-NOV-233949
27-NOV-234125
28-NOV-234249
29-NOV-234382
30-NOV-234646
01-DEC-234877
02-DEC-235120
03-DEC-235270
04-DEC-235479
05-DEC-235595
06-DEC-235758
07-DEC-235932
08-DEC-236097
09-DEC-236369
10-DEC-236642
11-DEC-236832
12-DEC-237093
13-DEC-237335
14-DEC-237610
15-DEC-237939
16-DEC-238191
17-DEC-238349
18-DEC-238478
19-DEC-238701
20-DEC-238946
21-DEC-239095
22-DEC-239427
23-DEC-239566
24-DEC-239685
25-DEC-239876
26-DEC-2310030
27-DEC-2310207
28-DEC-2310348
29-DEC-2310638
30-DEC-2310896
31-DEC-2311060
01-JAN-2411177
02-JAN-2411369
03-JAN-2411539
04-JAN-2411696
05-JAN-2411890
06-JAN-2412108
07-JAN-2412300
08-JAN-2412435
09-JAN-2412659
10-JAN-2412908
11-JAN-2413135
12-JAN-2413349
13-JAN-2413550
14-JAN-2413852
15-JAN-2414081
16-JAN-2414373
17-JAN-2414613
18-JAN-2414895
19-JAN-2415059
20-JAN-2415302
21-JAN-2415388
22-JAN-2415640
23-JAN-2415920
24-JAN-2416144
25-JAN-2416321
26-JAN-2416562
27-JAN-2416822
28-JAN-2416925
29-JAN-2417185
30-JAN-2417395
31-JAN-2417578
01-FEB-2417892
02-FEB-2418061
03-FEB-2418389
04-FEB-2418580
05-FEB-2418824
06-FEB-2419016

By placing the query above in parenthesis and treating it as a derived table, the first date for which the running daily total value is greater than or equal to 10,000 can be retrieved using Oracle’s FETCH clause. To ensure the correct date value is fetched, the derived table’s report date values need to be ordered in ascending order. See the final query and corresponding output below.

SELECT
    augmented_daily_report.report_date
FROM (
    SELECT
    	daily_report.report_date,
        SUM(SUM(daily_report.quantity_sold)) OVER (
        	ORDER BY daily_report.report_date
    		ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
        ) AS running_daily_total
    FROM
    	daily_report
    GROUP BY
        daily_report.report_date
) augmented_daily_report
WHERE
	augmented_daily_report.running_daily_total > 10000
ORDER BY
	augmented_daily_report.report_date
FETCH FIRST ROW ONLY;
report_date
26-DEC-23

The solution above makes use of Oracle’s FETCH clause. This clause should not be used when the row or rows returned can vary from execution to execution. For example, if there were multiple rows within the derived table, augmented_daily_report, with the date value of December 26th, 2023, then the specific row returned could vary. This would be noticeable with the corresponding running daily total values included in the output. As a result, the row returned may not be the one containing the lowest value meeting or exceeding the value of 10,000. For scenarios where the rows returned can vary due to ties in values, use DENSE_RANK or an ORDER BY clause resulting in consistent and predictable results.

7-day moving average
Generating a report containing 7-day moving averages for each cupcake flavor is straightforward compared to the previous problem. This can be achieved using the average function with the quantity sold attribute as its argument and a very specific OVER clause. Because the goal is to compute the 7-day moving average values for each cupcake flavor, the rows need to be partitioned by the flavor attribute. This will ensure each average is being computed using the quantity sold values corresponding to the specific flavor. Additionally, the OVER clause needs to consist of an ORDER BY clause to ensure each average value is being computed based on successive days. The final touch is to add a window clause specifying to compute each row’s average value based the quantity sold value within the row and those values within each of the six rows prior to it. Due to the way the data is structured in the daily report table and the PARTITION BY clause, each row translates to day. When the number of days (i.e., rows) existing prior to the current row’s day value is less than six, then the average is computed based on the current row’s quantity sold value and the available rows, if any. For example, the first date value in the daily report table is November 8, 2023. Each of the flavor’s 7-day moving average value is computed based on only the current row since none exist prior to it. Due to the nature of averages, the 7-day moving value and the quantity sold value will match. The solution described and the corresponding output can be found below. I have rounded the 7-day moving average values to two decimal places.

SELECT
    daily_report.report_date,
    daily_report.flavor,
    daily_report.quantity_sold,
    ROUND(
        AVG(daily_report.quantity_sold) OVER (
    		PARTITION BY daily_report.flavor
            ORDER BY daily_report.report_date
            ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
        ),
        2
    ) AS seven_day_moving_average
FROM
    daily_report
ORDER BY
    daily_report.report_date;
report_dateflavorquantity_soldseven_day_moving_average
08-NOV-23Chocolate1010
08-NOV-23Vanilla5555
08-NOV-23Red Velvet4949
08-NOV-23Lemon6969
09-NOV-23Red Velvet6858.5
09-NOV-23Vanilla8369
09-NOV-23Lemon4557
09-NOV-23Chocolate1814
10-NOV-23Chocolate9541
10-NOV-23Vanilla9778.33
10-NOV-23Red Velvet3450.33
10-NOV-23Lemon5255.33
11-NOV-23Red Velvet3947.5
11-NOV-23Vanilla6174
11-NOV-23Lemon1445
11-NOV-23Chocolate732.5
12-NOV-23Vanilla1261.6
12-NOV-23Lemon9054
12-NOV-23Chocolate5837.6
12-NOV-23Red Velvet9256.4
13-NOV-23Chocolate5240
13-NOV-23Lemon8459
13-NOV-23Vanilla5059.67
13-NOV-23Red Velvet6257.33
14-NOV-23Chocolate8546.43
14-NOV-23Red Velvet8761.57
14-NOV-23Vanilla5258.57
14-NOV-23Lemon6760.14
15-NOV-23Chocolate5252.43
15-NOV-23Red Velvet8666.86
15-NOV-23Vanilla8062.14
15-NOV-23Lemon2854.29
16-NOV-23Chocolate150
16-NOV-23Red Velvet958.43
16-NOV-23Vanilla4456.57
16-NOV-23Lemon7859
17-NOV-23Chocolate9449.86
17-NOV-23Red Velvet454.14
17-NOV-23Vanilla6351.71
17-NOV-23Lemon9765.43
18-NOV-23Chocolate7459.43
18-NOV-23Red Velvet4454.86
18-NOV-23Lemon2166.43
18-NOV-23Vanilla1445
19-NOV-23Chocolate5258.57
19-NOV-23Red Velvet842.86
19-NOV-23Lemon754.57
19-NOV-23Vanilla7854.43
20-NOV-23Chocolate5358.71
20-NOV-23Red Velvet3539
20-NOV-23Lemon9055.43
20-NOV-23Vanilla6957.14
21-NOV-23Chocolate1749
21-NOV-23Red Velvet1428.57
21-NOV-23Lemon7756.86
21-NOV-23Vanilla4756.43
22-NOV-23Chocolate4447.86
22-NOV-23Red Velvet4522.71
22-NOV-23Lemon7163
22-NOV-23Vanilla6754.57
23-NOV-23Chocolate5856
23-NOV-23Red Velvet7832.57
23-NOV-23Lemon7162
23-NOV-23Vanilla5556.14
24-NOV-23Chocolate4148.43
24-NOV-23Red Velvet5339.57
24-NOV-23Lemon1249.86
24-NOV-23Vanilla8158.71
25-NOV-23Chocolate3843.29
25-NOV-23Red Velvet5340.86
25-NOV-23Lemon3251.43
25-NOV-23Vanilla3061
26-NOV-23Chocolate3040.14
26-NOV-23Red Velvet7750.71
26-NOV-23Lemon150.57
26-NOV-23Vanilla8962.57
27-NOV-23Chocolate5039.71
27-NOV-23Red Velvet3550.71
27-NOV-23Lemon8650
27-NOV-23Vanilla553.43
28-NOV-23Chocolate8549.43
28-NOV-23Lemon1641.29
28-NOV-23Vanilla1749.14
28-NOV-23Red Velvet649.57
29-NOV-23Chocolate2046
29-NOV-23Lemon1433.14
29-NOV-23Vanilla9753.43
29-NOV-23Red Velvet243.43
30-NOV-23Chocolate5545.57
30-NOV-23Lemon9736.86
30-NOV-23Vanilla2048.43
30-NOV-23Red Velvet9245.43
01-DEC-23Chocolate8451.71
01-DEC-23Lemon7345.57
01-DEC-23Red Velvet1039.29
01-DEC-23Vanilla6446
02-DEC-23Vanilla8553.86
02-DEC-23Chocolate7757.29
02-DEC-23Lemon4347.14
02-DEC-23Red Velvet3837.14
03-DEC-23Red Velvet4232.14
03-DEC-23Chocolate3958.57
03-DEC-23Vanilla4647.71
03-DEC-23Lemon2350.29
04-DEC-23Chocolate9264.57
04-DEC-23Red Velvet6336.14
04-DEC-23Lemon1940.71
04-DEC-23Vanilla3552
05-DEC-23Chocolate5159.71
05-DEC-23Red Velvet4541.71
05-DEC-23Lemon038.43
05-DEC-23Vanilla2052.43
06-DEC-23Lemon4843.29
06-DEC-23Vanilla2341.86
06-DEC-23Red Velvet7652.29
06-DEC-23Chocolate1659.14
07-DEC-23Vanilla9552.57
07-DEC-23Red Velvet4545.57
07-DEC-23Chocolate2955.43
07-DEC-23Lemon530.14
08-DEC-23Vanilla5751.57
08-DEC-23Red Velvet4350.29
08-DEC-23Lemon1021.14
08-DEC-23Chocolate5551.29
09-DEC-23Vanilla9252.57
09-DEC-23Chocolate6349.29
09-DEC-23Lemon2919.14
09-DEC-23Red Velvet8857.43
10-DEC-23Vanilla9659.71
10-DEC-23Chocolate9457.14
10-DEC-23Lemon5724
10-DEC-23Red Velvet2655.14
11-DEC-23Vanilla2958.86
11-DEC-23Chocolate6553.29
11-DEC-23Lemon3526.29
11-DEC-23Red Velvet6154.86
12-DEC-23Vanilla6264.86
12-DEC-23Chocolate9960.14
12-DEC-23Lemon5834.57
12-DEC-23Red Velvet4254.43
13-DEC-23Vanilla2565.14
13-DEC-23Chocolate6867.57
13-DEC-23Lemon9741.57
13-DEC-23Red Velvet5251
14-DEC-23Vanilla9965.71
14-DEC-23Chocolate1765.86
14-DEC-23Lemon8152.43
14-DEC-23Red Velvet7855.71
15-DEC-23Vanilla9270.71
15-DEC-23Chocolate8069.43
15-DEC-23Lemon8162.57
15-DEC-23Red Velvet7660.43
16-DEC-23Vanilla8269.29
16-DEC-23Chocolate2964.57
16-DEC-23Lemon8270.14
16-DEC-23Red Velvet5956.29
17-DEC-23Vanilla1057
17-DEC-23Chocolate5559
17-DEC-23Lemon763
17-DEC-23Red Velvet8664.86
18-DEC-23Vanilla3057.14
18-DEC-23Chocolate250
18-DEC-23Lemon458.57
18-DEC-23Red Velvet9369.43
19-DEC-23Vanilla2551.86
19-DEC-23Chocolate8147.43
19-DEC-23Lemon4857.14
19-DEC-23Red Velvet6973.29
20-DEC-23Vanilla1750.71
20-DEC-23Chocolate8850.29
20-DEC-23Lemon9556.86
20-DEC-23Red Velvet4572.29
21-DEC-23Vanilla3741.86
21-DEC-23Chocolate3052.14
21-DEC-23Lemon6454.43
21-DEC-23Red Velvet1863.71
22-DEC-23Vanilla7339.14
22-DEC-23Chocolate10055
22-DEC-23Lemon6151.57
22-DEC-23Red Velvet9866.86
23-DEC-23Vanilla5234.86
23-DEC-23Chocolate151
23-DEC-23Lemon8151.43
23-DEC-23Red Velvet559.14
24-DEC-23Vanilla6342.43
24-DEC-23Chocolate3347.86
24-DEC-23Lemon2153.43
24-DEC-23Red Velvet247.14
25-DEC-23Vanilla4043.86
25-DEC-23Chocolate7858.71
25-DEC-23Lemon1254.57
25-DEC-23Red Velvet6142.57
26-DEC-23Vanilla8151.86
26-DEC-23Chocolate948.43
26-DEC-23Lemon4854.57
26-DEC-23Red Velvet1635
27-DEC-23Vanilla3053.71
27-DEC-23Chocolate5143.14
27-DEC-23Lemon8953.71
27-DEC-23Red Velvet729.57
28-DEC-23Vanilla4454.71
28-DEC-23Chocolate8150.43
28-DEC-23Lemon1646.86
28-DEC-23Red Velvet027
29-DEC-23Vanilla2848.29
29-DEC-23Chocolate8047.57
29-DEC-23Lemon9551.71
29-DEC-23Red Velvet8725.43
30-DEC-23Vanilla6750.43
30-DEC-23Chocolate7458
30-DEC-23Lemon3745.43
30-DEC-23Red Velvet8036.14
31-DEC-23Vanilla1443.43
31-DEC-23Chocolate2757.14
31-DEC-23Lemon7453
31-DEC-23Red Velvet4942.86
01-JAN-24Chocolate3851.43
01-JAN-24Red Velvet1336
01-JAN-24Lemon4557.71
01-JAN-24Vanilla2140.71
02-JAN-24Red Velvet1836.29
02-JAN-24Vanilla5036.29
02-JAN-24Lemon8162.43
02-JAN-24Chocolate4356.29
03-JAN-24Red Velvet1637.57
03-JAN-24Lemon2853.71
03-JAN-24Chocolate8360.86
03-JAN-24Vanilla4338.14
04-JAN-24Lemon2354.71
04-JAN-24Chocolate8261
04-JAN-24Vanilla132
04-JAN-24Red Velvet5144.86
05-JAN-24Chocolate650.43
05-JAN-24Lemon6149.86
05-JAN-24Red Velvet9445.86
05-JAN-24Vanilla3332.71
06-JAN-24Vanilla8835.71
06-JAN-24Lemon3249.14
06-JAN-24Chocolate9553.43
06-JAN-24Red Velvet334.86
07-JAN-24Lemon5746.71
07-JAN-24Chocolate7460.14
07-JAN-24Vanilla1135.29
07-JAN-24Red Velvet5035
08-JAN-24Vanilla2736.14
08-JAN-24Red Velvet2236.29
08-JAN-24Lemon8151.86
08-JAN-24Chocolate555.43
09-JAN-24Vanilla6638.43
09-JAN-24Chocolate550
09-JAN-24Red Velvet6442.86
09-JAN-24Lemon8953
10-JAN-24Vanilla5039.43
10-JAN-24Red Velvet8052
10-JAN-24Chocolate3543.14
10-JAN-24Lemon8461
11-JAN-24Chocolate8944.14
11-JAN-24Vanilla4746
11-JAN-24Red Velvet5152
11-JAN-24Lemon4063.43
12-JAN-24Vanilla9354.57
12-JAN-24Lemon5462.43
12-JAN-24Chocolate4349.43
12-JAN-24Red Velvet2442
13-JAN-24Chocolate10050.14
13-JAN-24Vanilla6250.86
13-JAN-24Red Velvet1744
13-JAN-24Lemon2261
14-JAN-24Lemon6962.71
14-JAN-24Vanilla10063.57
14-JAN-24Chocolate4045.29
14-JAN-24Red Velvet9350.14
15-JAN-24Lemon2554.71
15-JAN-24Chocolate7555.29
15-JAN-24Red Velvet5755.14
15-JAN-24Vanilla7270
16-JAN-24Lemon3446.86
16-JAN-24Chocolate9468
16-JAN-24Vanilla9474
16-JAN-24Red Velvet7056
17-JAN-24Chocolate3568
17-JAN-24Vanilla3571.86
17-JAN-24Red Velvet9357.86
17-JAN-24Lemon7745.86
18-JAN-24Red Velvet7861.71
18-JAN-24Vanilla9078
18-JAN-24Chocolate1757.71
18-JAN-24Lemon9754
19-JAN-24Lemon7156.43
19-JAN-24Chocolate3356.29
19-JAN-24Vanilla4471
19-JAN-24Red Velvet1660.57
20-JAN-24Chocolate1744.43
20-JAN-24Red Velvet9671.86
20-JAN-24Vanilla9575.71
20-JAN-24Lemon3558.29
21-JAN-24Chocolate539.43
21-JAN-24Vanilla261.71
21-JAN-24Red Velvet5566.43
21-JAN-24Lemon2451.86
22-JAN-24Red Velvet5766.43
22-JAN-24Chocolate9642.43
22-JAN-24Lemon8860.86
22-JAN-24Vanilla1153
23-JAN-24Vanilla7049.57
23-JAN-24Lemon8868.57
23-JAN-24Chocolate4835.86
23-JAN-24Red Velvet7467
24-JAN-24Red Velvet7264
24-JAN-24Vanilla4551
24-JAN-24Chocolate2334.14
24-JAN-24Lemon8469.57
25-JAN-24Vanilla4444.43
25-JAN-24Red Velvet3157.29
25-JAN-24Lemon8167.29
25-JAN-24Chocolate2134.71
26-JAN-24Vanilla4945.14
26-JAN-24Red Velvet9768.86
26-JAN-24Lemon9070
26-JAN-24Chocolate530.71
27-JAN-24Lemon9378.29
27-JAN-24Red Velvet756.14
27-JAN-24Chocolate6337.29
27-JAN-24Vanilla9745.43
28-JAN-24Lemon475.43
28-JAN-24Vanilla2849.14
28-JAN-24Chocolate2940.71
28-JAN-24Red Velvet4254.29
29-JAN-24Chocolate8739.43
29-JAN-24Lemon7072.86
29-JAN-24Vanilla548.29
29-JAN-24Red Velvet9860.14
30-JAN-24Chocolate5440.29
30-JAN-24Vanilla4845.14
30-JAN-24Red Velvet2152.57
30-JAN-24Lemon8772.71
31-JAN-24Vanilla9251.86
31-JAN-24Red Velvet442.86
31-JAN-24Lemon1162.29
31-JAN-24Chocolate7647.86
01-FEB-24Vanilla6855.29
01-FEB-24Red Velvet9351.71
01-FEB-24Lemon8863.29
01-FEB-24Chocolate6554.14
02-FEB-24Vanilla3653.43
02-FEB-24Red Velvet3743.14
02-FEB-24Chocolate7263.71
02-FEB-24Lemon2453.86
03-FEB-24Lemon9153.57
03-FEB-24Red Velvet5249.57
03-FEB-24Vanilla8952.29
03-FEB-24Chocolate9668.43
04-FEB-24Lemon8665.29
04-FEB-24Red Velvet444.14
04-FEB-24Vanilla10062.57
04-FEB-24Chocolate164.43
05-FEB-24Red Velvet9143.14
05-FEB-24Lemon7165.43
05-FEB-24Chocolate3456.86
05-FEB-24Vanilla4868.71
06-FEB-24Lemon6362
06-FEB-24Chocolate750.14
06-FEB-24Vanilla5770
06-FEB-24Red Velvet6549.43

Daily Cupcake Flavor Performance
There are several possible ways to generate the requested report. The methods for doing so have in common that each allows a within-row comparison to be made between the row’s quantity sold value and the value associated with the largest number of cupcakes ever sold for the row’s flavor value. Using the MAX function as a window function, the latter of the two values can be computed by simply partitioning the data by flavor. This ensures the maximum value computed is based on the quantity sold values in the data matching the current row’s flavor value. For each row, the difference can be computed by subtracting the result of the window function from the row’s quantity sold value. For clarity, see the code and corresponding output below.

SELECT
    daily_report.report_date,
    daily_report.flavor,
    daily_report.quantity_sold,
    MAX(daily_report.quantity_sold) OVER (
        PARTITION BY daily_report.flavor
    ) AS all_time_high,
    (
    	daily_report.quantity_sold
        - MAX(daily_report.quantity_sold) OVER (
            PARTITION BY daily_report.flavor
        )
    ) AS difference
FROM
    daily_report
ORDER BY
    daily_report.report_date;
report_dateflavorquantity_soldmonth_flavor_maxdifference
08-NOV-23Red Velvet4992-43
08-NOV-23Lemon6997-28
08-NOV-23Chocolate1095-85
08-NOV-23Vanilla5597-42
09-NOV-23Red Velvet6892-24
09-NOV-23Vanilla8397-14
09-NOV-23Lemon4597-52
09-NOV-23Chocolate1895-77
10-NOV-23Vanilla97970
10-NOV-23Red Velvet3492-58
10-NOV-23Chocolate95950
10-NOV-23Lemon5297-45
11-NOV-23Vanilla6197-36
11-NOV-23Red Velvet3992-53
11-NOV-23Lemon1497-83
11-NOV-23Chocolate795-88
12-NOV-23Chocolate5895-37
12-NOV-23Vanilla1297-85
12-NOV-23Red Velvet92920
12-NOV-23Lemon9097-7
13-NOV-23Chocolate5295-43
13-NOV-23Vanilla5097-47
13-NOV-23Red Velvet6292-30
13-NOV-23Lemon8497-13
14-NOV-23Lemon6797-30
14-NOV-23Vanilla5297-45
14-NOV-23Red Velvet8792-5
14-NOV-23Chocolate8595-10
15-NOV-23Red Velvet8692-6
15-NOV-23Vanilla8097-17
15-NOV-23Chocolate5295-43
15-NOV-23Lemon2897-69
16-NOV-23Chocolate195-94
16-NOV-23Vanilla4497-53
16-NOV-23Red Velvet992-83
16-NOV-23Lemon7897-19
17-NOV-23Lemon97970
17-NOV-23Vanilla6397-34
17-NOV-23Chocolate9495-1
17-NOV-23Red Velvet492-88
18-NOV-23Lemon2197-76
18-NOV-23Red Velvet4492-48
18-NOV-23Chocolate7495-21
18-NOV-23Vanilla1497-83
19-NOV-23Vanilla7897-19
19-NOV-23Chocolate5295-43
19-NOV-23Lemon797-90
19-NOV-23Red Velvet892-84
20-NOV-23Chocolate5395-42
20-NOV-23Vanilla6997-28
20-NOV-23Red Velvet3592-57
20-NOV-23Lemon9097-7
21-NOV-23Lemon7797-20
21-NOV-23Vanilla4797-50
21-NOV-23Red Velvet1492-78
21-NOV-23Chocolate1795-78
22-NOV-23Lemon7197-26
22-NOV-23Vanilla6797-30
22-NOV-23Chocolate4495-51
22-NOV-23Red Velvet4592-47
23-NOV-23Vanilla5597-42
23-NOV-23Red Velvet7892-14
23-NOV-23Lemon7197-26
23-NOV-23Chocolate5895-37
24-NOV-23Chocolate4195-54
24-NOV-23Vanilla8197-16
24-NOV-23Red Velvet5392-39
24-NOV-23Lemon1297-85
25-NOV-23Lemon3297-65
25-NOV-23Vanilla3097-67
25-NOV-23Chocolate3895-57
25-NOV-23Red Velvet5392-39
26-NOV-23Vanilla8997-8
26-NOV-23Chocolate3095-65
26-NOV-23Red Velvet7792-15
26-NOV-23Lemon197-96
27-NOV-23Vanilla597-92
27-NOV-23Lemon8697-11
27-NOV-23Chocolate5095-45
27-NOV-23Red Velvet3592-57
28-NOV-23Vanilla1797-80
28-NOV-23Chocolate8595-10
28-NOV-23Lemon1697-81
28-NOV-23Red Velvet692-86
29-NOV-23Lemon1497-83
29-NOV-23Red Velvet292-90
29-NOV-23Vanilla97970
29-NOV-23Chocolate2095-75
30-NOV-23Red Velvet92920
30-NOV-23Lemon97970
30-NOV-23Vanilla2097-77
30-NOV-23Chocolate5595-40
01-DEC-23Lemon7397-24
01-DEC-23Vanilla6499-35
01-DEC-23Chocolate84100-16
01-DEC-23Red Velvet1098-88
02-DEC-23Red Velvet3898-60
02-DEC-23Vanilla8599-14
02-DEC-23Chocolate77100-23
02-DEC-23Lemon4397-54
03-DEC-23Red Velvet4298-56
03-DEC-23Chocolate39100-61
03-DEC-23Lemon2397-74
03-DEC-23Vanilla4699-53
04-DEC-23Lemon1997-78
04-DEC-23Chocolate92100-8
04-DEC-23Red Velvet6398-35
04-DEC-23Vanilla3599-64
05-DEC-23Chocolate51100-49
05-DEC-23Lemon097-97
05-DEC-23Red Velvet4598-53
05-DEC-23Vanilla2099-79
06-DEC-23Red Velvet7698-22
06-DEC-23Vanilla2399-76
06-DEC-23Lemon4897-49
06-DEC-23Chocolate16100-84
07-DEC-23Lemon597-92
07-DEC-23Vanilla9599-4
07-DEC-23Chocolate29100-71
07-DEC-23Red Velvet4598-53
08-DEC-23Red Velvet4398-55
08-DEC-23Chocolate55100-45
08-DEC-23Vanilla5799-42
08-DEC-23Lemon1097-87
09-DEC-23Lemon2997-68
09-DEC-23Vanilla9299-7
09-DEC-23Chocolate63100-37
09-DEC-23Red Velvet8898-10
10-DEC-23Vanilla9699-3
10-DEC-23Red Velvet2698-72
10-DEC-23Lemon5797-40
10-DEC-23Chocolate94100-6
11-DEC-23Red Velvet6198-37
11-DEC-23Lemon3597-62
11-DEC-23Chocolate65100-35
11-DEC-23Vanilla2999-70
12-DEC-23Chocolate99100-1
12-DEC-23Vanilla6299-37
12-DEC-23Red Velvet4298-56
12-DEC-23Lemon5897-39
13-DEC-23Chocolate68100-32
13-DEC-23Lemon97970
13-DEC-23Vanilla2599-74
13-DEC-23Red Velvet5298-46
14-DEC-23Lemon8197-16
14-DEC-23Chocolate17100-83
14-DEC-23Vanilla99990
14-DEC-23Red Velvet7898-20
15-DEC-23Chocolate80100-20
15-DEC-23Lemon8197-16
15-DEC-23Red Velvet7698-22
15-DEC-23Vanilla9299-7
16-DEC-23Vanilla8299-17
16-DEC-23Red Velvet5998-39
16-DEC-23Lemon8297-15
16-DEC-23Chocolate29100-71
17-DEC-23Vanilla1099-89
17-DEC-23Red Velvet8698-12
17-DEC-23Lemon797-90
17-DEC-23Chocolate55100-45
18-DEC-23Red Velvet9398-5
18-DEC-23Vanilla3099-69
18-DEC-23Lemon497-93
18-DEC-23Chocolate2100-98
19-DEC-23Lemon4897-49
19-DEC-23Vanilla2599-74
19-DEC-23Red Velvet6998-29
19-DEC-23Chocolate81100-19
20-DEC-23Lemon9597-2
20-DEC-23Red Velvet4598-53
20-DEC-23Vanilla1799-82
20-DEC-23Chocolate88100-12
21-DEC-23Vanilla3799-62
21-DEC-23Chocolate30100-70
21-DEC-23Lemon6497-33
21-DEC-23Red Velvet1898-80
22-DEC-23Lemon6197-36
22-DEC-23Chocolate1001000
22-DEC-23Vanilla7399-26
22-DEC-23Red Velvet98980
23-DEC-23Lemon8197-16
23-DEC-23Vanilla5299-47
23-DEC-23Red Velvet598-93
23-DEC-23Chocolate1100-99
24-DEC-23Vanilla6399-36
24-DEC-23Red Velvet298-96
24-DEC-23Chocolate33100-67
24-DEC-23Lemon2197-76
25-DEC-23Lemon1297-85
25-DEC-23Chocolate78100-22
25-DEC-23Red Velvet6198-37
25-DEC-23Vanilla4099-59
26-DEC-23Red Velvet1698-82
26-DEC-23Lemon4897-49
26-DEC-23Chocolate9100-91
26-DEC-23Vanilla8199-18
27-DEC-23Red Velvet798-91
27-DEC-23Lemon8997-8
27-DEC-23Chocolate51100-49
27-DEC-23Vanilla3099-69
28-DEC-23Red Velvet098-98
28-DEC-23Chocolate81100-19
28-DEC-23Lemon1697-81
28-DEC-23Vanilla4499-55
29-DEC-23Red Velvet8798-11
29-DEC-23Vanilla2899-71
29-DEC-23Lemon9597-2
29-DEC-23Chocolate80100-20
30-DEC-23Red Velvet8098-18
30-DEC-23Vanilla6799-32
30-DEC-23Lemon3797-60
30-DEC-23Chocolate74100-26
31-DEC-23Vanilla1499-85
31-DEC-23Lemon7497-23
31-DEC-23Red Velvet4998-49
31-DEC-23Chocolate27100-73
01-JAN-24Red Velvet1398-85
01-JAN-24Lemon4597-52
01-JAN-24Chocolate38100-62
01-JAN-24Vanilla21100-79
02-JAN-24Red Velvet1898-80
02-JAN-24Lemon8197-16
02-JAN-24Chocolate43100-57
02-JAN-24Vanilla50100-50
03-JAN-24Red Velvet1698-82
03-JAN-24Lemon2897-69
03-JAN-24Chocolate83100-17
03-JAN-24Vanilla43100-57
04-JAN-24Red Velvet5198-47
04-JAN-24Lemon2397-74
04-JAN-24Chocolate82100-18
04-JAN-24Vanilla1100-99
05-JAN-24Red Velvet9498-4
05-JAN-24Lemon6197-36
05-JAN-24Chocolate6100-94
05-JAN-24Vanilla33100-67
06-JAN-24Red Velvet398-95
06-JAN-24Lemon3297-65
06-JAN-24Chocolate95100-5
06-JAN-24Vanilla88100-12
07-JAN-24Red Velvet5098-48
07-JAN-24Lemon5797-40
07-JAN-24Chocolate74100-26
07-JAN-24Vanilla11100-89
08-JAN-24Red Velvet2298-76
08-JAN-24Lemon8197-16
08-JAN-24Chocolate5100-95
08-JAN-24Vanilla27100-73
09-JAN-24Red Velvet6498-34
09-JAN-24Lemon8997-8
09-JAN-24Chocolate5100-95
09-JAN-24Vanilla66100-34
10-JAN-24Red Velvet8098-18
10-JAN-24Lemon8497-13
10-JAN-24Chocolate35100-65
10-JAN-24Vanilla50100-50
11-JAN-24Red Velvet5198-47
11-JAN-24Lemon4097-57
11-JAN-24Chocolate89100-11
11-JAN-24Vanilla47100-53
12-JAN-24Vanilla93100-7
12-JAN-24Red Velvet2498-74
12-JAN-24Lemon5497-43
12-JAN-24Chocolate43100-57
13-JAN-24Red Velvet1798-81
13-JAN-24Lemon2297-75
13-JAN-24Chocolate1001000
13-JAN-24Vanilla62100-38
14-JAN-24Red Velvet9398-5
14-JAN-24Lemon6997-28
14-JAN-24Chocolate40100-60
14-JAN-24Vanilla1001000
15-JAN-24Red Velvet5798-41
15-JAN-24Lemon2597-72
15-JAN-24Chocolate75100-25
15-JAN-24Vanilla72100-28
16-JAN-24Red Velvet7098-28
16-JAN-24Lemon3497-63
16-JAN-24Chocolate94100-6
16-JAN-24Vanilla94100-6
17-JAN-24Red Velvet9398-5
17-JAN-24Lemon7797-20
17-JAN-24Chocolate35100-65
17-JAN-24Vanilla35100-65
18-JAN-24Red Velvet7898-20
18-JAN-24Lemon97970
18-JAN-24Chocolate17100-83
18-JAN-24Vanilla90100-10
19-JAN-24Red Velvet1698-82
19-JAN-24Lemon7197-26
19-JAN-24Chocolate33100-67
19-JAN-24Vanilla44100-56
20-JAN-24Red Velvet9698-2
20-JAN-24Lemon3597-62
20-JAN-24Chocolate17100-83
20-JAN-24Vanilla95100-5
21-JAN-24Red Velvet5598-43
21-JAN-24Lemon2497-73
21-JAN-24Chocolate5100-95
21-JAN-24Vanilla2100-98
22-JAN-24Vanilla11100-89
22-JAN-24Chocolate96100-4
22-JAN-24Lemon8897-9
22-JAN-24Red Velvet5798-41
23-JAN-24Lemon8897-9
23-JAN-24Chocolate48100-52
23-JAN-24Vanilla70100-30
23-JAN-24Red Velvet7498-24
24-JAN-24Lemon8497-13
24-JAN-24Chocolate23100-77
24-JAN-24Vanilla45100-55
24-JAN-24Red Velvet7298-26
25-JAN-24Vanilla44100-56
25-JAN-24Lemon8197-16
25-JAN-24Red Velvet3198-67
25-JAN-24Chocolate21100-79
26-JAN-24Vanilla49100-51
26-JAN-24Lemon9097-7
26-JAN-24Red Velvet9798-1
26-JAN-24Chocolate5100-95
27-JAN-24Vanilla97100-3
27-JAN-24Chocolate63100-37
27-JAN-24Lemon9397-4
27-JAN-24Red Velvet798-91
28-JAN-24Chocolate29100-71
28-JAN-24Lemon497-93
28-JAN-24Vanilla28100-72
28-JAN-24Red Velvet4298-56
29-JAN-24Lemon7097-27
29-JAN-24Red Velvet98980
29-JAN-24Vanilla5100-95
29-JAN-24Chocolate87100-13
30-JAN-24Red Velvet2198-77
30-JAN-24Chocolate54100-46
30-JAN-24Lemon8797-10
30-JAN-24Vanilla48100-52
31-JAN-24Lemon1197-86
31-JAN-24Vanilla92100-8
31-JAN-24Red Velvet498-94
31-JAN-24Chocolate76100-24
01-FEB-24Chocolate6596-31
01-FEB-24Vanilla68100-32
01-FEB-24Red Velvet93930
01-FEB-24Lemon8891-3
02-FEB-24Vanilla36100-64
02-FEB-24Chocolate7296-24
02-FEB-24Red Velvet3793-56
02-FEB-24Lemon2491-67
03-FEB-24Vanilla89100-11
03-FEB-24Chocolate96960
03-FEB-24Red Velvet5293-41
03-FEB-24Lemon91910
04-FEB-24Chocolate196-95
04-FEB-24Vanilla1001000
04-FEB-24Lemon8691-5
04-FEB-24Red Velvet493-89
05-FEB-24Red Velvet9193-2
05-FEB-24Vanilla48100-52
05-FEB-24Chocolate3496-62
05-FEB-24Lemon7191-20
06-FEB-24Chocolate796-89
06-FEB-24Lemon6391-28
06-FEB-24Vanilla57100-43
06-FEB-24Red Velvet6593-28

Knowledge Check
If you’d like to confirm your understanding on the use of aggregate functions with windows and other fundamental concepts, consider solving a handful of my practice problems here.