ROW REFERENCE FUNCTIONS

To the surprise of many learning SQL, the methods for referencing a specific attribute value within a specific row are not as straightforward as those afforded by the popular spreadsheet software, Microsoft Excel. Using Excel, a user can simply click on an intersection created by a row and column (i.e., a cell) or manually type in the desired cell’s reference value (e.g., C10 for column C, row 10) to make use of the value. While not as simple, SQL users can do the same using SELF JOINS or window/analytic functions. The window functions, LAG and LEAD, were introduced specifically to allow users to achieve the same outcome produced by the somewhat tricky SELF JOIN operation. LAG allows users to access rows prior to the position of the current row while LEAD allows users to access rows beyond the position of the current row. The functions allow a user to specify the offset or number of rows to look back (LAG) or forward (LEAD) relative to the current row along with a default value to be captured in those rows where a row does not exist prior (LAG) to or after (LEAD) the current row. The functions operate on sorted rows with the sorting specified by the user in the OVER clause. If you are unfamiliar with the general components of window functions, I strongly recommend first reading my page that provides a high-level overview here. Let us walk through two real-world examples highlighting the usefulness of these intuitive functions.

The data used for these two examples 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. The data set contains daily sales for software, hardware, and office supplies product categories within the year 2023. There is one row per product category and day value combination.

report_dateproduct_categorytotal_sale
01-JAN-23Software130986
01-JAN-23Hardware325957
01-JAN-23Office Supplies359381
02-JAN-23Software350039
02-JAN-23Hardware235789
02-JAN-23Office Supplies291373
03-JAN-23Software463015
03-JAN-23Hardware382062
03-JAN-23Office Supplies106383
04-JAN-23Software499010
04-JAN-23Hardware116633
04-JAN-23Office Supplies441220
05-JAN-23Software482840
05-JAN-23Hardware456018
05-JAN-23Office Supplies308338
06-JAN-23Software463402
06-JAN-23Hardware385487
06-JAN-23Office Supplies244131
07-JAN-23Software247090
07-JAN-23Hardware198045
07-JAN-23Office Supplies359830
08-JAN-23Software372801
08-JAN-23Hardware471010
08-JAN-23Office Supplies450011
09-JAN-23Software401816
09-JAN-23Hardware466308
09-JAN-23Office Supplies119207
10-JAN-23Software146721
10-JAN-23Hardware314424
10-JAN-23Office Supplies291614
11-JAN-23Software184358
11-JAN-23Hardware447629
11-JAN-23Office Supplies445219
12-JAN-23Software122224
12-JAN-23Hardware491773
12-JAN-23Office Supplies377890
13-JAN-23Software216145
13-JAN-23Hardware212298
13-JAN-23Office Supplies269156
14-JAN-23Software117248
14-JAN-23Hardware353179
14-JAN-23Office Supplies488083
15-JAN-23Software130295
15-JAN-23Hardware468557
15-JAN-23Office Supplies122763
16-JAN-23Software295801
16-JAN-23Hardware487853
16-JAN-23Office Supplies459475
17-JAN-23Software184603
17-JAN-23Hardware275430
17-JAN-23Office Supplies222975
18-JAN-23Software421602
18-JAN-23Hardware191991
18-JAN-23Office Supplies280727
19-JAN-23Software388766
19-JAN-23Hardware432802
19-JAN-23Office Supplies292265
20-JAN-23Software378589
20-JAN-23Hardware396760
20-JAN-23Office Supplies450214
21-JAN-23Software482988
21-JAN-23Hardware147373
21-JAN-23Office Supplies329691
22-JAN-23Software451602
22-JAN-23Hardware121235
22-JAN-23Office Supplies108407
23-JAN-23Software319111
23-JAN-23Hardware198986
23-JAN-23Office Supplies168317
24-JAN-23Software477495
24-JAN-23Hardware480650
24-JAN-23Office Supplies238667
25-JAN-23Software232214
25-JAN-23Hardware277385
25-JAN-23Office Supplies166603
26-JAN-23Software382594
26-JAN-23Hardware168602
26-JAN-23Office Supplies458864
27-JAN-23Software409486
27-JAN-23Hardware486419
27-JAN-23Office Supplies241738
28-JAN-23Software242183
28-JAN-23Hardware468323
28-JAN-23Office Supplies406416
29-JAN-23Software472342
29-JAN-23Hardware473124
29-JAN-23Office Supplies380432
30-JAN-23Software237485
30-JAN-23Hardware400814
30-JAN-23Office Supplies388534
31-JAN-23Software328104
31-JAN-23Hardware199133
31-JAN-23Office Supplies259672
01-FEB-23Software498848
01-FEB-23Hardware468851
01-FEB-23Office Supplies374415
02-FEB-23Software496490
02-FEB-23Hardware349406
02-FEB-23Office Supplies167543
03-FEB-23Software191080
03-FEB-23Hardware273296
03-FEB-23Office Supplies468504
04-FEB-23Software198142
04-FEB-23Hardware316817
04-FEB-23Office Supplies452816
05-FEB-23Software289578
05-FEB-23Hardware304197
05-FEB-23Office Supplies260594
06-FEB-23Software239591
06-FEB-23Hardware298252
06-FEB-23Office Supplies419222
07-FEB-23Software434004
07-FEB-23Hardware120772
07-FEB-23Office Supplies365084
08-FEB-23Software356630
08-FEB-23Hardware455330
08-FEB-23Office Supplies120497
09-FEB-23Software210124
09-FEB-23Hardware189088
09-FEB-23Office Supplies422049
10-FEB-23Software396941
10-FEB-23Hardware147215
10-FEB-23Office Supplies298119
11-FEB-23Software327989
11-FEB-23Hardware476343
11-FEB-23Office Supplies449502
12-FEB-23Software113082
12-FEB-23Hardware228704
12-FEB-23Office Supplies126792
13-FEB-23Software234147
13-FEB-23Hardware473675
13-FEB-23Office Supplies341899
14-FEB-23Software427368
14-FEB-23Hardware107628
14-FEB-23Office Supplies303236
15-FEB-23Software194554
15-FEB-23Hardware431761
15-FEB-23Office Supplies272520
16-FEB-23Software167010
16-FEB-23Hardware211933
16-FEB-23Office Supplies271376
17-FEB-23Software299654
17-FEB-23Hardware171489
17-FEB-23Office Supplies421586
18-FEB-23Software253618
18-FEB-23Hardware184734
18-FEB-23Office Supplies154463
19-FEB-23Software280170
19-FEB-23Hardware208972
19-FEB-23Office Supplies157939
20-FEB-23Software296464
20-FEB-23Hardware393431
20-FEB-23Office Supplies396622
21-FEB-23Software365663
21-FEB-23Hardware419070
21-FEB-23Office Supplies249639
22-FEB-23Software418007
22-FEB-23Hardware211224
22-FEB-23Office Supplies445522
23-FEB-23Software479608
23-FEB-23Hardware423726
23-FEB-23Office Supplies277872
24-FEB-23Software102493
24-FEB-23Hardware166960
24-FEB-23Office Supplies305881
25-FEB-23Software222458
25-FEB-23Hardware128558
25-FEB-23Office Supplies452534
26-FEB-23Software137605
26-FEB-23Hardware423640
26-FEB-23Office Supplies167263
27-FEB-23Software291874
27-FEB-23Hardware409779
27-FEB-23Office Supplies260577
28-FEB-23Software343636
28-FEB-23Hardware150560
28-FEB-23Office Supplies231950
01-MAR-23Software352583
01-MAR-23Hardware108159
01-MAR-23Office Supplies185315
02-MAR-23Software107441
02-MAR-23Hardware309547
02-MAR-23Office Supplies122136
03-MAR-23Software423414
03-MAR-23Hardware499811
03-MAR-23Office Supplies392746
04-MAR-23Software491539
04-MAR-23Hardware345375
04-MAR-23Office Supplies218853
05-MAR-23Software248758
05-MAR-23Hardware174163
05-MAR-23Office Supplies355488
06-MAR-23Software450393
06-MAR-23Hardware169503
06-MAR-23Office Supplies278893
07-MAR-23Software477258
07-MAR-23Hardware422113
07-MAR-23Office Supplies200048
08-MAR-23Software374121
08-MAR-23Hardware291224
08-MAR-23Office Supplies108375
09-MAR-23Software221727
09-MAR-23Hardware472045
09-MAR-23Office Supplies118751
10-MAR-23Software318517
10-MAR-23Hardware140101
10-MAR-23Office Supplies443992
11-MAR-23Software128573
11-MAR-23Hardware218247
11-MAR-23Office Supplies427230
12-MAR-23Software334955
12-MAR-23Hardware425448
12-MAR-23Office Supplies420772
13-MAR-23Software467659
13-MAR-23Hardware403023
13-MAR-23Office Supplies423537
14-MAR-23Software170618
14-MAR-23Hardware494032
14-MAR-23Office Supplies412336
15-MAR-23Software424735
15-MAR-23Hardware371217
15-MAR-23Office Supplies202721
16-MAR-23Software308023
16-MAR-23Hardware487999
16-MAR-23Office Supplies493143
17-MAR-23Software346157
17-MAR-23Hardware269133
17-MAR-23Office Supplies331892
18-MAR-23Software360626
18-MAR-23Hardware217758
18-MAR-23Office Supplies341784
19-MAR-23Software240326
19-MAR-23Hardware474311
19-MAR-23Office Supplies480205
20-MAR-23Software204067
20-MAR-23Hardware325958
20-MAR-23Office Supplies349648
21-MAR-23Software466128
21-MAR-23Hardware451988
21-MAR-23Office Supplies218058
22-MAR-23Software319976
22-MAR-23Hardware326494
22-MAR-23Office Supplies270824
23-MAR-23Software139625
23-MAR-23Hardware216417
23-MAR-23Office Supplies477186
24-MAR-23Software279025
24-MAR-23Hardware121012
24-MAR-23Office Supplies163536
25-MAR-23Software191230
25-MAR-23Hardware401993
25-MAR-23Office Supplies293330
26-MAR-23Software302770
26-MAR-23Hardware182144
26-MAR-23Office Supplies279223
27-MAR-23Software101519
27-MAR-23Hardware467885
27-MAR-23Office Supplies241178
28-MAR-23Software350644
28-MAR-23Hardware179143
28-MAR-23Office Supplies257859
29-MAR-23Software285776
29-MAR-23Hardware268899
29-MAR-23Office Supplies192559
30-MAR-23Software407435
30-MAR-23Hardware439023
30-MAR-23Office Supplies251407
31-MAR-23Software270420
31-MAR-23Hardware433787
31-MAR-23Office Supplies355011
01-APR-23Software141595
01-APR-23Hardware390594
01-APR-23Office Supplies320527
02-APR-23Software183160
02-APR-23Hardware464361
02-APR-23Office Supplies487635
03-APR-23Software179908
03-APR-23Hardware487048
03-APR-23Office Supplies109011
04-APR-23Software156679
04-APR-23Hardware437387
04-APR-23Office Supplies171126
05-APR-23Software125223
05-APR-23Hardware163396
05-APR-23Office Supplies299903
06-APR-23Software121008
06-APR-23Hardware241845
06-APR-23Office Supplies442197
07-APR-23Software221383
07-APR-23Hardware454711
07-APR-23Office Supplies405102
08-APR-23Software107507
08-APR-23Hardware251905
08-APR-23Office Supplies220887
09-APR-23Software310617
09-APR-23Hardware227411
09-APR-23Office Supplies265518
10-APR-23Software422232
10-APR-23Hardware310046
10-APR-23Office Supplies150205
11-APR-23Software332198
11-APR-23Hardware320621
11-APR-23Office Supplies411607
12-APR-23Software384063
12-APR-23Hardware274390
12-APR-23Office Supplies366354
13-APR-23Software280966
13-APR-23Hardware382678
13-APR-23Office Supplies169193
14-APR-23Software288234
14-APR-23Hardware158199
14-APR-23Office Supplies405273
15-APR-23Software312305
15-APR-23Hardware375867
15-APR-23Office Supplies242540
16-APR-23Software457762
16-APR-23Hardware306784
16-APR-23Office Supplies410744
17-APR-23Software134756
17-APR-23Hardware128819
17-APR-23Office Supplies393735
18-APR-23Software156509
18-APR-23Hardware277928
18-APR-23Office Supplies185693
19-APR-23Software475899
19-APR-23Hardware352213
19-APR-23Office Supplies118006
20-APR-23Software486045
20-APR-23Hardware105392
20-APR-23Office Supplies274407
21-APR-23Software137841
21-APR-23Hardware412107
21-APR-23Office Supplies307669
22-APR-23Software420618
22-APR-23Hardware440743
22-APR-23Office Supplies211778
23-APR-23Software437481
23-APR-23Hardware306189
23-APR-23Office Supplies446075
24-APR-23Software369096
24-APR-23Hardware309242
24-APR-23Office Supplies300044
25-APR-23Software347470
25-APR-23Hardware433689
25-APR-23Office Supplies330579
26-APR-23Software147642
26-APR-23Hardware465270
26-APR-23Office Supplies458689
27-APR-23Software131632
27-APR-23Hardware345373
27-APR-23Office Supplies256230
28-APR-23Software159253
28-APR-23Hardware478742
28-APR-23Office Supplies487975
29-APR-23Software235898
29-APR-23Hardware308098
29-APR-23Office Supplies172834
30-APR-23Software429613
30-APR-23Hardware370109
30-APR-23Office Supplies279783
01-MAY-23Software140762
01-MAY-23Hardware318807
01-MAY-23Office Supplies294785
02-MAY-23Software376862
02-MAY-23Hardware208853
02-MAY-23Office Supplies498942
03-MAY-23Software117052
03-MAY-23Hardware249786
03-MAY-23Office Supplies182057
04-MAY-23Software188616
04-MAY-23Hardware326858
04-MAY-23Office Supplies116027
05-MAY-23Software210789
05-MAY-23Hardware382226
05-MAY-23Office Supplies462508
06-MAY-23Software224315
06-MAY-23Hardware204152
06-MAY-23Office Supplies243199
07-MAY-23Software144383
07-MAY-23Hardware107532
07-MAY-23Office Supplies197586
08-MAY-23Software274237
08-MAY-23Hardware145299
08-MAY-23Office Supplies484134
09-MAY-23Software262382
09-MAY-23Hardware273740
09-MAY-23Office Supplies220205
10-MAY-23Software380503
10-MAY-23Hardware350232
10-MAY-23Office Supplies310852
11-MAY-23Software391562
11-MAY-23Hardware478243
11-MAY-23Office Supplies124996
12-MAY-23Software240861
12-MAY-23Hardware245958
12-MAY-23Office Supplies418096
13-MAY-23Software298986
13-MAY-23Hardware364523
13-MAY-23Office Supplies183475
14-MAY-23Software412636
14-MAY-23Hardware236258
14-MAY-23Office Supplies292129
15-MAY-23Software474717
15-MAY-23Hardware242421
15-MAY-23Office Supplies227600
16-MAY-23Software218739
16-MAY-23Hardware283568
16-MAY-23Office Supplies182894
17-MAY-23Software231174
17-MAY-23Hardware280282
17-MAY-23Office Supplies315630
18-MAY-23Software270421
18-MAY-23Hardware203850
18-MAY-23Office Supplies415408
19-MAY-23Software263918
19-MAY-23Hardware303145
19-MAY-23Office Supplies492547
20-MAY-23Software414991
20-MAY-23Hardware257366
20-MAY-23Office Supplies459085
21-MAY-23Software309794
21-MAY-23Hardware408615
21-MAY-23Office Supplies228029
22-MAY-23Software207054
22-MAY-23Hardware329477
22-MAY-23Office Supplies472817
23-MAY-23Software434124
23-MAY-23Hardware409775
23-MAY-23Office Supplies246750
24-MAY-23Software145983
24-MAY-23Hardware136951
24-MAY-23Office Supplies340411
25-MAY-23Software435328
25-MAY-23Hardware119100
25-MAY-23Office Supplies249954
26-MAY-23Software325187
26-MAY-23Hardware392976
26-MAY-23Office Supplies328867
27-MAY-23Software167029
27-MAY-23Hardware393557
27-MAY-23Office Supplies454022
28-MAY-23Software435836
28-MAY-23Hardware150924
28-MAY-23Office Supplies454083
29-MAY-23Software226260
29-MAY-23Hardware155481
29-MAY-23Office Supplies281388
30-MAY-23Software117544
30-MAY-23Hardware155852
30-MAY-23Office Supplies403325
31-MAY-23Software377182
31-MAY-23Hardware108781
31-MAY-23Office Supplies273138
01-JUN-23Software311505
01-JUN-23Hardware119690
01-JUN-23Office Supplies465736
02-JUN-23Software264947
02-JUN-23Hardware408841
02-JUN-23Office Supplies152197
03-JUN-23Software374350
03-JUN-23Hardware264722
03-JUN-23Office Supplies320520
04-JUN-23Software423306
04-JUN-23Hardware166503
04-JUN-23Office Supplies299382
05-JUN-23Software465584
05-JUN-23Hardware495608
05-JUN-23Office Supplies496827
06-JUN-23Software244276
06-JUN-23Hardware330948
06-JUN-23Office Supplies196702
07-JUN-23Software446569
07-JUN-23Hardware350827
07-JUN-23Office Supplies308290
08-JUN-23Software413169
08-JUN-23Hardware436055
08-JUN-23Office Supplies464097
09-JUN-23Software409417
09-JUN-23Hardware224598
09-JUN-23Office Supplies385330
10-JUN-23Software376142
10-JUN-23Hardware311306
10-JUN-23Office Supplies418557
11-JUN-23Software419889
11-JUN-23Hardware357489
11-JUN-23Office Supplies156642
12-JUN-23Software306148
12-JUN-23Hardware200276
12-JUN-23Office Supplies427942
13-JUN-23Software302151
13-JUN-23Hardware199538
13-JUN-23Office Supplies157699
14-JUN-23Software149388
14-JUN-23Hardware490336
14-JUN-23Office Supplies460060
15-JUN-23Software253405
15-JUN-23Hardware401420
15-JUN-23Office Supplies146533
16-JUN-23Software450910
16-JUN-23Hardware370536
16-JUN-23Office Supplies386430
17-JUN-23Software378090
17-JUN-23Hardware464114
17-JUN-23Office Supplies406680
18-JUN-23Software211615
18-JUN-23Hardware290351
18-JUN-23Office Supplies444837
19-JUN-23Software237235
19-JUN-23Hardware220923
19-JUN-23Office Supplies244289
20-JUN-23Software351066
20-JUN-23Hardware141090
20-JUN-23Office Supplies220148
21-JUN-23Software470754
21-JUN-23Hardware294240
21-JUN-23Office Supplies122212
22-JUN-23Software377163
22-JUN-23Hardware229455
22-JUN-23Office Supplies266779
23-JUN-23Software227325
23-JUN-23Hardware267736
23-JUN-23Office Supplies195147
24-JUN-23Software154526
24-JUN-23Hardware293664
24-JUN-23Office Supplies321285
25-JUN-23Software156763
25-JUN-23Hardware199975
25-JUN-23Office Supplies252248
26-JUN-23Software354824
26-JUN-23Hardware364079
26-JUN-23Office Supplies306592
27-JUN-23Software350527
27-JUN-23Hardware287508
27-JUN-23Office Supplies188713
28-JUN-23Software292010
28-JUN-23Hardware487758
28-JUN-23Office Supplies101658
29-JUN-23Software363394
29-JUN-23Hardware157124
29-JUN-23Office Supplies478412
30-JUN-23Software300932
30-JUN-23Hardware157215
30-JUN-23Office Supplies241366
01-JUL-23Software148090
01-JUL-23Hardware272906
01-JUL-23Office Supplies393778
02-JUL-23Software179911
02-JUL-23Hardware426551
02-JUL-23Office Supplies219791
03-JUL-23Software226839
03-JUL-23Hardware380730
03-JUL-23Office Supplies169157
04-JUL-23Software241680
04-JUL-23Hardware105436
04-JUL-23Office Supplies164201
05-JUL-23Software207715
05-JUL-23Hardware434853
05-JUL-23Office Supplies164089
06-JUL-23Software158928
06-JUL-23Hardware466440
06-JUL-23Office Supplies154431
07-JUL-23Software251429
07-JUL-23Hardware487763
07-JUL-23Office Supplies408431
08-JUL-23Software333003
08-JUL-23Hardware143077
08-JUL-23Office Supplies128848
09-JUL-23Software221806
09-JUL-23Hardware334149
09-JUL-23Office Supplies351364
10-JUL-23Software100624
10-JUL-23Hardware178095
10-JUL-23Office Supplies286670
11-JUL-23Software408145
11-JUL-23Hardware275415
11-JUL-23Office Supplies440642
12-JUL-23Software488925
12-JUL-23Hardware234437
12-JUL-23Office Supplies220215
13-JUL-23Software441077
13-JUL-23Hardware283602
13-JUL-23Office Supplies480705
14-JUL-23Software321405
14-JUL-23Hardware496504
14-JUL-23Office Supplies369516
15-JUL-23Software370793
15-JUL-23Hardware458243
15-JUL-23Office Supplies222362
16-JUL-23Software252803
16-JUL-23Hardware350939
16-JUL-23Office Supplies454198
17-JUL-23Software156089
17-JUL-23Hardware114823
17-JUL-23Office Supplies144888
18-JUL-23Software386844
18-JUL-23Hardware133935
18-JUL-23Office Supplies200292
19-JUL-23Software270214
19-JUL-23Hardware269897
19-JUL-23Office Supplies107055
20-JUL-23Software245143
20-JUL-23Hardware180536
20-JUL-23Office Supplies104647
21-JUL-23Software406462
21-JUL-23Hardware134985
21-JUL-23Office Supplies156146
22-JUL-23Software109799
22-JUL-23Hardware230605
22-JUL-23Office Supplies239874
23-JUL-23Software284416
23-JUL-23Hardware148793
23-JUL-23Office Supplies218456
24-JUL-23Software144795
24-JUL-23Hardware380334
24-JUL-23Office Supplies462944
25-JUL-23Software423947
25-JUL-23Hardware122222
25-JUL-23Office Supplies446006
26-JUL-23Software130794
26-JUL-23Hardware485806
26-JUL-23Office Supplies394017
27-JUL-23Software483047
27-JUL-23Hardware277896
27-JUL-23Office Supplies348972
28-JUL-23Software396253
28-JUL-23Hardware387469
28-JUL-23Office Supplies212031
29-JUL-23Software386963
29-JUL-23Hardware178360
29-JUL-23Office Supplies445313
30-JUL-23Software447697
30-JUL-23Hardware234068
30-JUL-23Office Supplies314974
31-JUL-23Software224862
31-JUL-23Hardware347539
31-JUL-23Office Supplies318588
01-AUG-23Software136851
01-AUG-23Hardware331205
01-AUG-23Office Supplies227110
02-AUG-23Software109391
02-AUG-23Hardware155210
02-AUG-23Office Supplies107036
03-AUG-23Software340818
03-AUG-23Hardware297598
03-AUG-23Office Supplies231274
04-AUG-23Software417143
04-AUG-23Hardware480037
04-AUG-23Office Supplies137046
05-AUG-23Software490894
05-AUG-23Hardware417638
05-AUG-23Office Supplies119741
06-AUG-23Software494310
06-AUG-23Hardware253262
06-AUG-23Office Supplies447793
07-AUG-23Software356028
07-AUG-23Hardware141396
07-AUG-23Office Supplies468005
08-AUG-23Software216678
08-AUG-23Hardware293425
08-AUG-23Office Supplies213345
09-AUG-23Software101459
09-AUG-23Hardware457496
09-AUG-23Office Supplies364674
10-AUG-23Software454849
10-AUG-23Hardware409279
10-AUG-23Office Supplies396333
11-AUG-23Software437044
11-AUG-23Hardware181646
11-AUG-23Office Supplies211539
12-AUG-23Software190055
12-AUG-23Hardware433339
12-AUG-23Office Supplies177433
13-AUG-23Software453043
13-AUG-23Hardware371613
13-AUG-23Office Supplies283404
14-AUG-23Software125292
14-AUG-23Hardware400191
14-AUG-23Office Supplies257934
15-AUG-23Software386019
15-AUG-23Hardware387147
15-AUG-23Office Supplies305108
16-AUG-23Software231773
16-AUG-23Hardware381273
16-AUG-23Office Supplies331623
17-AUG-23Software393107
17-AUG-23Hardware303725
17-AUG-23Office Supplies275465
18-AUG-23Software282980
18-AUG-23Hardware341541
18-AUG-23Office Supplies140965
19-AUG-23Software431933
19-AUG-23Hardware138311
19-AUG-23Office Supplies288702
20-AUG-23Software491784
20-AUG-23Hardware464240
20-AUG-23Office Supplies464490
21-AUG-23Software403369
21-AUG-23Hardware277863
21-AUG-23Office Supplies379244
22-AUG-23Software342814
22-AUG-23Hardware107198
22-AUG-23Office Supplies413377
23-AUG-23Software214480
23-AUG-23Hardware443937
23-AUG-23Office Supplies289252
24-AUG-23Software303146
24-AUG-23Hardware119602
24-AUG-23Office Supplies153454
25-AUG-23Software205728
25-AUG-23Hardware242047
25-AUG-23Office Supplies428543
26-AUG-23Software273114
26-AUG-23Hardware348451
26-AUG-23Office Supplies174699
27-AUG-23Software444968
27-AUG-23Hardware394566
27-AUG-23Office Supplies261222
28-AUG-23Software140139
28-AUG-23Hardware237829
28-AUG-23Office Supplies250821
29-AUG-23Software437298
29-AUG-23Hardware368978
29-AUG-23Office Supplies219958
30-AUG-23Software400241
30-AUG-23Hardware181840
30-AUG-23Office Supplies397579
31-AUG-23Software141923
31-AUG-23Hardware356412
31-AUG-23Office Supplies149477
01-SEP-23Software162649
01-SEP-23Hardware368106
01-SEP-23Office Supplies407390
02-SEP-23Software171311
02-SEP-23Hardware100499
02-SEP-23Office Supplies331085
03-SEP-23Software494360
03-SEP-23Hardware434919
03-SEP-23Office Supplies400876
04-SEP-23Software385077
04-SEP-23Hardware498835
04-SEP-23Office Supplies445773
05-SEP-23Software204009
05-SEP-23Hardware456094
05-SEP-23Office Supplies189993
06-SEP-23Software215665
06-SEP-23Hardware376902
06-SEP-23Office Supplies432878
07-SEP-23Software449924
07-SEP-23Hardware131924
07-SEP-23Office Supplies202070
08-SEP-23Software215311
08-SEP-23Hardware340668
08-SEP-23Office Supplies146842
09-SEP-23Software499203
09-SEP-23Hardware243056
09-SEP-23Office Supplies189038
10-SEP-23Software310956
10-SEP-23Hardware256403
10-SEP-23Office Supplies300350
11-SEP-23Software338729
11-SEP-23Hardware365795
11-SEP-23Office Supplies387708
12-SEP-23Software460844
12-SEP-23Hardware277039
12-SEP-23Office Supplies242547
13-SEP-23Software259629
13-SEP-23Hardware267474
13-SEP-23Office Supplies283371
14-SEP-23Software475576
14-SEP-23Hardware330046
14-SEP-23Office Supplies393402
15-SEP-23Software206995
15-SEP-23Hardware244148
15-SEP-23Office Supplies193924
16-SEP-23Software340814
16-SEP-23Hardware152964
16-SEP-23Office Supplies245881
17-SEP-23Software152836
17-SEP-23Hardware350166
17-SEP-23Office Supplies213764
18-SEP-23Software499650
18-SEP-23Hardware257235
18-SEP-23Office Supplies197081
19-SEP-23Software196319
19-SEP-23Hardware161852
19-SEP-23Office Supplies111162
20-SEP-23Software496428
20-SEP-23Hardware382268
20-SEP-23Office Supplies256903
21-SEP-23Software131435
21-SEP-23Hardware333089
21-SEP-23Office Supplies300714
22-SEP-23Software288585
22-SEP-23Hardware345921
22-SEP-23Office Supplies275874
23-SEP-23Software188320
23-SEP-23Hardware363638
23-SEP-23Office Supplies223569
24-SEP-23Software373364
24-SEP-23Hardware191241
24-SEP-23Office Supplies206948
25-SEP-23Software326280
25-SEP-23Hardware156920
25-SEP-23Office Supplies276073
26-SEP-23Software295994
26-SEP-23Hardware456126
26-SEP-23Office Supplies393633
27-SEP-23Software292724
27-SEP-23Hardware152039
27-SEP-23Office Supplies493222
28-SEP-23Software302803
28-SEP-23Hardware310606
28-SEP-23Office Supplies413639
29-SEP-23Software397432
29-SEP-23Hardware435049
29-SEP-23Office Supplies427647
30-SEP-23Software398871
30-SEP-23Hardware457273
30-SEP-23Office Supplies159307
01-OCT-23Software399450
01-OCT-23Hardware291064
01-OCT-23Office Supplies100564
02-OCT-23Software484086
02-OCT-23Hardware264161
02-OCT-23Office Supplies175967
03-OCT-23Software169276
03-OCT-23Hardware295316
03-OCT-23Office Supplies107787
04-OCT-23Software317494
04-OCT-23Hardware214179
04-OCT-23Office Supplies244205
05-OCT-23Software352830
05-OCT-23Hardware379116
05-OCT-23Office Supplies407086
06-OCT-23Software389838
06-OCT-23Hardware295644
06-OCT-23Office Supplies213362
07-OCT-23Software490714
07-OCT-23Hardware389043
07-OCT-23Office Supplies213891
08-OCT-23Software104384
08-OCT-23Hardware299232
08-OCT-23Office Supplies192875
09-OCT-23Software170542
09-OCT-23Hardware428867
09-OCT-23Office Supplies268138
10-OCT-23Software228362
10-OCT-23Hardware187456
10-OCT-23Office Supplies303195
11-OCT-23Software335181
11-OCT-23Hardware487771
11-OCT-23Office Supplies154703
12-OCT-23Software224134
12-OCT-23Hardware357450
12-OCT-23Office Supplies355402
13-OCT-23Software282916
13-OCT-23Hardware395557
13-OCT-23Office Supplies352236
14-OCT-23Software283861
14-OCT-23Hardware113488
14-OCT-23Office Supplies170306
15-OCT-23Software137838
15-OCT-23Hardware145554
15-OCT-23Office Supplies431156
16-OCT-23Software400308
16-OCT-23Hardware192641
16-OCT-23Office Supplies106251
17-OCT-23Software127001
17-OCT-23Hardware388146
17-OCT-23Office Supplies324092
18-OCT-23Software141539
18-OCT-23Hardware403256
18-OCT-23Office Supplies256505
19-OCT-23Software252182
19-OCT-23Hardware469993
19-OCT-23Office Supplies219932
20-OCT-23Software268703
20-OCT-23Hardware212448
20-OCT-23Office Supplies351617
21-OCT-23Software379162
21-OCT-23Hardware404458
21-OCT-23Office Supplies283087
22-OCT-23Software162490
22-OCT-23Hardware441628
22-OCT-23Office Supplies471630
23-OCT-23Software499608
23-OCT-23Hardware135746
23-OCT-23Office Supplies274674
24-OCT-23Software259322
24-OCT-23Hardware173699
24-OCT-23Office Supplies309133
25-OCT-23Software283668
25-OCT-23Hardware128553
25-OCT-23Office Supplies434598
26-OCT-23Software145047
26-OCT-23Hardware404693
26-OCT-23Office Supplies391874
27-OCT-23Software199126
27-OCT-23Hardware197544
27-OCT-23Office Supplies317014
28-OCT-23Software492231
28-OCT-23Hardware269902
28-OCT-23Office Supplies490712
29-OCT-23Software459700
29-OCT-23Hardware487364
29-OCT-23Office Supplies457764
30-OCT-23Software274636
30-OCT-23Hardware392838
30-OCT-23Office Supplies469899
31-OCT-23Software207020
31-OCT-23Hardware162079
31-OCT-23Office Supplies300016
01-NOV-23Software135323
01-NOV-23Hardware346351
01-NOV-23Office Supplies455117
02-NOV-23Software141936
02-NOV-23Hardware137447
02-NOV-23Office Supplies181300
03-NOV-23Software205830
03-NOV-23Hardware159631
03-NOV-23Office Supplies266341
04-NOV-23Software315384
04-NOV-23Hardware310670
04-NOV-23Office Supplies416700
05-NOV-23Software258691
05-NOV-23Hardware186587
05-NOV-23Office Supplies307950
06-NOV-23Software148380
06-NOV-23Hardware351308
06-NOV-23Office Supplies167538
07-NOV-23Software436947
07-NOV-23Hardware260935
07-NOV-23Office Supplies382351
08-NOV-23Software342330
08-NOV-23Hardware338863
08-NOV-23Office Supplies391822
09-NOV-23Software240852
09-NOV-23Hardware337127
09-NOV-23Office Supplies334466
10-NOV-23Software441530
10-NOV-23Hardware206628
10-NOV-23Office Supplies197825
11-NOV-23Software474690
11-NOV-23Hardware294645
11-NOV-23Office Supplies420051
12-NOV-23Software264250
12-NOV-23Hardware325605
12-NOV-23Office Supplies166001
13-NOV-23Software115899
13-NOV-23Hardware250878
13-NOV-23Office Supplies464325
14-NOV-23Software158215
14-NOV-23Hardware414510
14-NOV-23Office Supplies408215
15-NOV-23Software233715
15-NOV-23Hardware250923
15-NOV-23Office Supplies356489
16-NOV-23Software298662
16-NOV-23Hardware108081
16-NOV-23Office Supplies338672
17-NOV-23Software125303
17-NOV-23Hardware211584
17-NOV-23Office Supplies153773
18-NOV-23Software352250
18-NOV-23Hardware449350
18-NOV-23Office Supplies400943
19-NOV-23Software191838
19-NOV-23Hardware276175
19-NOV-23Office Supplies183478
20-NOV-23Software289584
20-NOV-23Hardware483466
20-NOV-23Office Supplies244075
21-NOV-23Software279126
21-NOV-23Hardware180520
21-NOV-23Office Supplies354276
22-NOV-23Software186392
22-NOV-23Hardware479634
22-NOV-23Office Supplies136275
23-NOV-23Software482701
23-NOV-23Hardware274590
23-NOV-23Office Supplies337465
24-NOV-23Software272275
24-NOV-23Hardware206596
24-NOV-23Office Supplies265819
25-NOV-23Software475753
25-NOV-23Hardware170663
25-NOV-23Office Supplies411858
26-NOV-23Software238840
26-NOV-23Hardware140992
26-NOV-23Office Supplies346945
27-NOV-23Software230495
27-NOV-23Hardware266155
27-NOV-23Office Supplies448711
28-NOV-23Software388240
28-NOV-23Hardware293781
28-NOV-23Office Supplies155978
29-NOV-23Software498768
29-NOV-23Hardware166529
29-NOV-23Office Supplies470821
30-NOV-23Software103530
30-NOV-23Hardware453834
30-NOV-23Office Supplies309072
01-DEC-23Software310076
01-DEC-23Hardware295026
01-DEC-23Office Supplies331398
02-DEC-23Software318602
02-DEC-23Hardware244608
02-DEC-23Office Supplies394145
03-DEC-23Software444490
03-DEC-23Hardware216417
03-DEC-23Office Supplies425513
04-DEC-23Software193954
04-DEC-23Hardware470937
04-DEC-23Office Supplies214677
05-DEC-23Software104492
05-DEC-23Hardware101056
05-DEC-23Office Supplies282247
06-DEC-23Software465632
06-DEC-23Hardware491091
06-DEC-23Office Supplies490342
07-DEC-23Software247888
07-DEC-23Hardware322334
07-DEC-23Office Supplies442330
08-DEC-23Software132190
08-DEC-23Hardware177824
08-DEC-23Office Supplies277247
09-DEC-23Software300054
09-DEC-23Hardware277895
09-DEC-23Office Supplies247049
10-DEC-23Software325098
10-DEC-23Hardware189922
10-DEC-23Office Supplies433469
11-DEC-23Software345347
11-DEC-23Hardware292778
11-DEC-23Office Supplies469616
12-DEC-23Software168864
12-DEC-23Hardware490877
12-DEC-23Office Supplies351204
13-DEC-23Software159965
13-DEC-23Hardware420244
13-DEC-23Office Supplies287081
14-DEC-23Software337371
14-DEC-23Hardware332795
14-DEC-23Office Supplies111930
15-DEC-23Software461622
15-DEC-23Hardware234988
15-DEC-23Office Supplies267212
16-DEC-23Software230958
16-DEC-23Hardware353872
16-DEC-23Office Supplies284872
17-DEC-23Software146321
17-DEC-23Hardware246657
17-DEC-23Office Supplies388863
18-DEC-23Software413152
18-DEC-23Hardware135720
18-DEC-23Office Supplies131658
19-DEC-23Software486320
19-DEC-23Hardware110131
19-DEC-23Office Supplies472921
20-DEC-23Software478448
20-DEC-23Hardware143700
20-DEC-23Office Supplies334531
21-DEC-23Software327614
21-DEC-23Hardware289838
21-DEC-23Office Supplies409196
22-DEC-23Software395130
22-DEC-23Hardware262819
22-DEC-23Office Supplies461814
23-DEC-23Software465882
23-DEC-23Hardware164457
23-DEC-23Office Supplies421300
24-DEC-23Software469328
24-DEC-23Hardware303003
24-DEC-23Office Supplies323886
25-DEC-23Software102272
25-DEC-23Hardware209510
25-DEC-23Office Supplies457322
26-DEC-23Software209543
26-DEC-23Hardware263148
26-DEC-23Office Supplies431696
27-DEC-23Software462120
27-DEC-23Hardware346375
27-DEC-23Office Supplies424553
28-DEC-23Software135913
28-DEC-23Hardware238250
28-DEC-23Office Supplies225642
29-DEC-23Software465128
29-DEC-23Hardware331667
29-DEC-23Office Supplies302910
30-DEC-23Software442538
30-DEC-23Hardware147313
30-DEC-23Office Supplies134577
31-DEC-23Software185736
31-DEC-23Hardware387580
31-DEC-23Office Supplies210082
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 north_america_sale AS
    WITH north_america_sale (report_date, product_category, total_sale) AS (
        SELECT
            daily_sale.report_date,
            data_template.product_category,
            daily_sale.total_sale
        FROM (
            SELECT
               'Software' AS category_1,
               'Hardware' AS category_2,
               'Office Supplies' AS category_3
            FROM
                dual
        )
        UNPIVOT (
            product_category FOR category_column IN (
                "CATEGORY_1", "CATEGORY_2", "CATEGORY_3"
            )
        ) data_template
        CROSS JOIN (
        SELECT
            TO_DATE('2023-01-01', 'YYYY-MM-DD') AS report_date,
            FLOOR(DBMS_RANDOM.VALUE(100000, 500000)) AS total_sale
        FROM
            dual
        ) daily_sale
        UNION ALL
        SELECT
            north_america_sale.report_date + INTERVAL '1' DAY,
            north_america_sale.product_category,
            FLOOR(DBMS_RANDOM.VALUE(100000, 500000))
        FROM
            north_america_sale
        WHERE
            north_america_sale.report_date < TO_DATE('2023-12-31', 'YYYY-MM-DD')
    )
    SELECT
        north_america_sale.report_date,
        north_america_sale.product_category,
        north_america_sale.total_sale
    FROM
        north_america_sale;

North American Sales
Suppose you are a reporting analyst for a large company focused on the sale of various software, hardware, and office supply products. Today, you were assigned a ticket in your Service Now queue requesting two reports to be developed intended to answer the following questions:

Report 1
– How much sales were generated within each month?
– For each month, how much more or less sales were generated in the prior month?
– For each month, what was the percent change, rounded to two decimal places, in sales relative to the previous month?

Report 2
– For each product category, how much sales were generated within each month?
– For each product category, how much more or less sales were generated in the prior month?
– For each product category, what was the percent change, rounded to two decimal places, in sales relative to the previous month?

Generating Report 1
Based on the questions listed for report 1, you will need to develop a query that will return a result set containing the total sales by month. To do so, the SUM function can be used in the SELECT clause along with an attribute derived based on the truncation of each report date value. Using the TRUNC function with the report date attribute and MONTH as arguments results in each original report date’s day value being the first of the corresponding month. A GROUP BY clause consisting of the same derived attribute results in the total sales by month. The current query can be seen below along with the corresponding result set. While not entirely necessarily, I have sorted the result set by reporting month values for ease of use.

SELECT
    TRUNC(north_america_sale.report_date, 'MONTH') AS reporting_month,
    SUM(north_america_sale.total_sale) AS total_sale
FROM
    north_america_sale
GROUP BY
    TRUNC(north_america_sale.report_date, 'MONTH')
ORDER BY
    reporting_month;
reporting_monthtotal_sale
01-JAN-2330092600
01-FEB-2325048255
01-MAR-2328982026
01-APR-2327177069
01-MAY-2326798754
01-JUN-2327854615
01-JUL-2326549507
01-AUG-2328133612
01-SEP-2327473052
01-OCT-2327481206
01-NOV-2326136439
01-DEC-2328666163

The result set above answers the first question, but does not contain the necessary data to easily answer the remaining two questions. Ideally, each row in the result set above should contain the corresponding previous month’s sales, the difference between the current month and previous month, and the associated month-over-month percent change. This will minimize the amount of mental or cognitive effort required of your report’s consumers. To compute and return the previous month’s sales relative to each reporting month value, the LAG function can be used. Because the goal is to capture the previous month’s sales within each row, the expression specified or passed into the function should be the sum of sales and the offset should be 1. When computing the previous month’s sales for the first month in the ordered data, the default value of NULL will be returned. See the augmented query and corresponding result set below.

SELECT
    TRUNC(north_america_sale.report_date, 'MONTH') AS reporting_month,
    SUM(north_america_sale.total_sale) AS total_sale,
    LAG(SUM(north_america_sale.total_sale), 1, NULL) OVER (
    	ORDER BY TRUNC(north_america_sale.report_date, 'MONTH')
    ) AS previous_month_total_sale
FROM
    north_america_sale
GROUP BY
	TRUNC(north_america_sale.report_date, 'MONTH')
ORDER BY
    reporting_month;
reporting_monthtotal_saleprevious_month_total_sale
01-JAN-2330092600
01-FEB-232504825530092600
01-MAR-232898202625048255
01-APR-232717706928982026
01-MAY-232679875427177069
01-JUN-232785461526798754
01-JUL-232654950727854615
01-AUG-232813361226549507
01-SEP-232747305228133612
01-OCT-232748120627473052
01-NOV-232613643927481206
01-DEC-232866616326136439

In the result set above, each month’s total sale value along with the previous month’s total sale value is captured. For example, the total sales occurring in February 2023 along with January 2023 is returned within a single row. As expected, this is the case for every month aside for January 2023 where only the total sale value is available due to December 2022 not being available in the fictional data set.

Given the result set, updating the query to also include an attribute capturing the difference between the current month’s sales and the previous month’s is straightforward. This can be achieved by simply using the subtract operator or minus sign. See the updated query and corresponding result set below.

SELECT
    TRUNC(north_america_sale.report_date, 'MONTH') AS reporting_month,
    SUM(north_america_sale.total_sale) AS total_sale,
    LAG(SUM(north_america_sale.total_sale), 1, NULL) OVER (
    	ORDER BY TRUNC(north_america_sale.report_date, 'MONTH')
    ) AS previous_month_total_sale,
    (
        SUM(north_america_sale.total_sale)
    	- LAG(SUM(north_america_sale.total_sale), 1, NULL) OVER (
        	ORDER BY TRUNC(north_america_sale.report_date, 'MONTH')
        )
    ) AS difference
FROM
    north_america_sale
GROUP BY
	TRUNC(north_america_sale.report_date, 'MONTH')
ORDER BY
    reporting_month;
reporting_monthtotal_saleprevious_month_total_saledifference
01-JAN-2330092600
01-FEB-232504825530092600-5044345
01-MAR-2328982026250482553933771
01-APR-232717706928982026-1804957
01-MAY-232679875427177069-378315
01-JUN-2327854615267987541055861
01-JUL-232654950727854615-1305108
01-AUG-2328133612265495071584105
01-SEP-232747305228133612-660560
01-OCT-2327481206274730528154
01-NOV-232613643927481206-1344767
01-DEC-2328666163261364392529724

Now, the result set is only missing an attribute capturing the month-over-month percent change. To compute this measure for each month, the resulting difference between the current month’s sales and previous month’s sales can be divided by the current month’s sales and subsequently multiplied by 100. Each percent change value can then be rounded to two decimal places using the ROUND function. For more information on the ROUND function along with other numeric functions, see my page dedicated to them here. The formula, query, and corresponding output can be found below.

Formula
((current month’s sales – previous month’s sales) / current month’s sales)) * 100

SELECT
    TRUNC(north_america_sale.report_date, 'MONTH') AS reporting_month,
    SUM(north_america_sale.total_sale) AS total_sale,
    LAG(SUM(north_america_sale.total_sale), 1, NULL) OVER (
    	ORDER BY TRUNC(north_america_sale.report_date, 'MONTH')
    ) AS previous_month_total_sale,
    (
        SUM(north_america_sale.total_sale)
    	- LAG(SUM(north_america_sale.total_sale), 1, NULL) OVER (
        	ORDER BY TRUNC(north_america_sale.report_date, 'MONTH')
        )
    ) AS difference,
    ROUND(
        (
            (
                SUM(north_america_sale.total_sale)
            	- LAG(SUM(north_america_sale.total_sale), 1, NULL) OVER (
                	ORDER BY TRUNC(north_america_sale.report_date, 'MONTH')
                )
            ) / SUM(north_america_sale.total_sale)
        ) * 100,
    	2
    ) AS percent_change
FROM
    north_america_sale
GROUP BY
	TRUNC(north_america_sale.report_date, 'MONTH')
ORDER BY
    reporting_month;
reporting_monthtotal_saleprevious_month_total_saledifferencepercent_change
01-JAN-2330092600
01-FEB-232504825530092600-5044345-20.14
01-MAR-232898202625048255393377113.57
01-APR-232717706928982026-1804957-6.64
01-MAY-232679875427177069-378315-1.41
01-JUN-23278546152679875410558613.79
01-JUL-232654950727854615-1305108-4.92
01-AUG-23281336122654950715841055.63
01-SEP-232747305228133612-660560-2.4
01-OCT-2327481206274730528154.03
01-NOV-232613643927481206-1344767-5.15
01-DEC-23286661632613643925297248.82

Having generated the result set above, report 1 is ready. At the beginning of this discussion, I mentioned there were two common strategies used for solving problems requiring the ability to reference values within other rows. The one utilized above uses window functions and is aligned with the focus of this page. For those curious readers, I have also written a SELECT statement that generates the same result set but with the use of a SELF JOIN. To review the code, click on the icon to the left of the section header below. To hide the code, click the icon again.

SELF JOIN
WITH monthly_sale AS (
    SELECT
        TRUNC(north_america_sale.report_date, 'MONTH') AS reporting_month,
        SUM(north_america_sale.total_sale) AS total_sale
    FROM
        north_america_sale
    GROUP BY
        TRUNC(north_america_sale.report_date, 'MONTH')
)
SELECT
    current_month.reporting_month,
    current_month.total_sale AS current_month_sale,
    previous_month.total_sale AS previous_month_sale,
    (
    	current_month.total_sale
    	- previous_month.total_sale
    ) AS difference,
    ROUND(
        (
            (
            	current_month.total_sale
            	- previous_month.total_sale
            ) / current_month.total_sale
        
        ) * 100,
    	2
    ) AS percent_change
FROM
    monthly_sale current_month
    LEFT JOIN
    monthly_sale previous_month
    	ON current_month.reporting_month = ADD_MONTHS(previous_month.reporting_month, 1)
ORDER BY
	current_month.reporting_month;

Generating Report 2
Developing a query to generate report 2 requires a few simple updates to report 1’s query. Let us walk through the updates together using a top-down approach.

The report requires the product category attribute to be included in the result set, so the attribute needs to be added to the SELECT clause. Additionally, the measures computed need to account for this newly added attribute in their own ways. The aggregate function, SUM, requires the GROUP BY clause to be updated to include the product category attribute while measures using the window function, LAG, require the OVER clause to include a PARTITION BY clause consisting of the product category attribute. Adding the product category attribute to the GROUP BY clause ensures the total sale measure represents the total sales corresponding to the row’s reporting month and product category value. By including a PARTITION BY clause consisting of the product category attribute, the LAG function operates on each subset of rows defined by their product category values. This ensures, when the data set is ordered by reporting month, the previous month value returned is not simply the value within the row prior to the current row, but the value within the row prior to the current row with the same product category value. At this point, all that is left is to update the ORDER BY clause to include the product category attribute to make it easier to consume the report. For clarity, see the code and corresponding output below.

SELECT
    TRUNC(north_america_sale.report_date, 'MONTH') AS reporting_month,
    north_america_sale.product_category,
    SUM(north_america_sale.total_sale) AS total_sale,
    LAG(SUM(north_america_sale.total_sale), 1, NULL) OVER (
    	PARTITION BY north_america_sale.product_category
    	ORDER BY TRUNC(north_america_sale.report_date, 'MONTH')
    ) AS previous_month_total_sale,
    (
        SUM(north_america_sale.total_sale)
    	- LAG(SUM(north_america_sale.total_sale), 1, NULL) OVER (
        	PARTITION BY north_america_sale.product_category
        	ORDER BY TRUNC(north_america_sale.report_date, 'MONTH')
        )
    ) AS difference,
    ROUND(
        (
            (
                SUM(north_america_sale.total_sale)
            	- LAG(SUM(north_america_sale.total_sale), 1, NULL) OVER (
    				PARTITION BY north_america_sale.product_category
                	ORDER BY TRUNC(north_america_sale.report_date, 'MONTH')
                )
            ) / SUM(north_america_sale.total_sale)
        ) * 100,
    	2
    ) AS percent_change
FROM
    north_america_sale
GROUP BY
	TRUNC(north_america_sale.report_date, 'MONTH'),
    north_america_sale.product_category
ORDER BY
    reporting_month,
    north_america_sale.product_category;
reporting_monthproduct_categorytotal_saleprevious_month_total_saledifferencepercent_change
01-JAN-23Hardware10532049
01-JAN-23Office Supplies9527596
01-JAN-23Software10032955
01-FEB-23Hardware814541110532049-2386638-29.3
01-FEB-23Office Supplies86360169527596-891580-10.32
01-FEB-23Software826682810032955-1766127-21.36
01-MAR-23Hardware101079528145411196254119.42
01-MAR-23Office Supplies930803686360166720207.22
01-MAR-23Software95660388266828129921013.58
01-APR-23Hardware998115710107952-126795-1.27
01-APR-23Office Supplies91013199308036-206717-2.27
01-APR-23Software80945939566038-1471445-18.18
01-MAY-23Hardware82245889981157-1756569-21.36
01-MAY-23Office Supplies985493991013197536207.65
01-MAY-23Software871922780945936246347.16
01-JUN-23Hardware898392582245887593378.45
01-JUN-23Office Supplies90333109854939-821629-9.1
01-JUN-23Software98373808719227111815311.37
01-JUL-23Hardware89564088983925-27517-.31
01-JUL-23Office Supplies87426019033310-290709-3.33
01-JUL-23Software88504989837380-986882-11.15
01-AUG-23Hardware971829589564087618877.84
01-AUG-23Office Supplies85666468742601-175955-2.05
01-AUG-23Software9848671885049899817310.14
01-SEP-23Hardware91982959718295-520000-5.65
01-SEP-23Office Supplies874266485666461760182.01
01-SEP-23Software95320939848671-316578-3.32
01-OCT-23Hardware940888691982952105912.24
01-OCT-23Office Supplies914967187426644070074.45
01-OCT-23Software89226499532093-609444-6.83
01-NOV-23Hardware83340589408886-1074828-12.9
01-NOV-23Office Supplies947465291496713249813.43
01-NOV-23Software83277298922649-594920-7.14
01-DEC-23Hardware849283283340581587741.87
01-DEC-23Office Supplies1044128394746529666319.26
01-DEC-23Software97320488327729140431914.43

Advertisement Campaign Changes
Suppose the company for which you are employed has been using the same advertisement campaign to promote company products for several years. Eager to test changes, your company created slightly different advertisement campaigns for quarters two through four in year 2023. With the year having ended, management now wishes to review the effect on total sales for each quarter given the slight changes made in advertising before the start of the each subsequent quarter. You have been verbally requested to generate a report containing each quarter’s total sales along with the following quarter’s sales to allow management to easily trace the effects of the minor changes in advertising. Management mentioned the following requirements related to the structure of the report:

– Each quarter’s total sales and the following quarter’s total sales should be captured within a single row.
– Within each row, the first quarter’s total sales should be captured. This will allow management to more easily compare the sales generated with the historical ad campaign in the first quarter to those generated using the slightly modified campaigns in subsequent quarters.
– The difference between the following quarter’s total sales and the first quarter’s total sales should be included in the report along with the percent change in total sales observed when comparing the following quarter’s total sales to the first quarter’s total sales. The percent change in total sales should be rounded to two decimal places.

Generating the Report
Based on the requirements set forth by management, you will need to develop a query that will return a result set containing the total sales by quarter. To do so, the SUM function can be used in the SELECT clause along with an attribute derived based on the quarter in which each report date falls. Using the TO_CHAR function with the report date attribute and Q as arguments results in each original report date value being the corresponding quarter number (e.g., 1, 2, 3, 4). A GROUP BY clause consisting of the same derived attribute results in the total sales by quarter. To include the next quarter’s sales value within each row, the LEAD function can be used with a value of 1 specified as the offset. The ORDER BY clause within the OVER clause is significant in that it ensures the value representing the next quarter’s total sales is accurate. The current query can be seen below along with the corresponding result set. Like the previous example, the query has been sorted. In this case, by quarter value. A next quarter total sale value does not exist for the row corresponding to quarter 4 because the fictional data set only includes year 2023.

SELECT
    TO_CHAR(north_america_sale.report_date, 'Q') AS reporting_quarter,
    SUM(north_america_sale.total_sale) AS total_sale,
        LEAD(SUM(north_america_sale.total_sale), 1, NULL) OVER (
    	ORDER BY TO_CHAR(north_america_sale.report_date, 'Q')
    ) AS next_quarter_total_sale
FROM
    north_america_sale
GROUP BY
    TO_CHAR(north_america_sale.report_date, 'Q')
ORDER BY
    TO_CHAR(north_america_sale.report_date, 'Q');
reporting_quartertotal_salenext_quarter_total_sale
18412288181830438
28183043882156171
38215617182283808
482283808

The result set produced by the query above is a good start to meeting the report’s requirements. One of the remaining requirements is to include the total sales value associated with quarter 1 in all rows. There are multiple ways to achieve this, but in the spirit of using window functions, let us use the FIRST_VALUE function. This function allows us to retrieve the value within the first row of a set of ordered rows. Using this same strategy, the difference between the first quarter’s sales and each subsequent quarter can be computed. The same applies to computing the percent change but with a little help from the ROUND function. See the code and corresponding output below.

SELECT
    TO_CHAR(north_america_sale.report_date, 'Q') AS reporting_quarter,
    SUM(north_america_sale.total_sale) AS total_sale,
    FIRST_VALUE(SUM(north_america_sale.total_sale)) OVER (
        ORDER BY TO_CHAR(north_america_sale.report_date, 'Q')
        ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    ) AS first_quarter_total_sale,
    LEAD(SUM(north_america_sale.total_sale), 1, NULL) OVER (
    	ORDER BY TO_CHAR(north_america_sale.report_date, 'Q')
    ) AS next_quarter_sale,
    (
        FIRST_VALUE(SUM(north_america_sale.total_sale)) OVER (
            ORDER BY TO_CHAR(north_america_sale.report_date, 'Q')
            ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
        )
    	-  LEAD(SUM(north_america_sale.total_sale), 1, NULL) OVER (
    	ORDER BY TO_CHAR(north_america_sale.report_date, 'Q')
    	)
    ) AS difference,
    ROUND(
        (
            (	LEAD(SUM(north_america_sale.total_sale), 1, NULL) OVER (
                	ORDER BY TO_CHAR(north_america_sale.report_date, 'Q')
                )
                - FIRST_VALUE(SUM(north_america_sale.total_sale)) OVER (
    				ORDER BY TO_CHAR(north_america_sale.report_date, 'Q')
    				ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
                )
            ) / LEAD(SUM(north_america_sale.total_sale), 1, NULL) OVER (
                	ORDER BY TO_CHAR(north_america_sale.report_date, 'Q')
                )
        ) * 100,
    	2
    ) AS percent_change
FROM
    north_america_sale
GROUP BY
    TO_CHAR(north_america_sale.report_date, 'Q')
ORDER BY
    TO_CHAR(north_america_sale.report_date, 'Q');
reporting_quartertotal_salefirst_quarter_total_salenext_quarter_total_saledifferencepercent_change
18412288184122881818304382292443-2.8
28183043884122881821561711966710-2.39
38215617184122881822838081839073-2.24
48228380884122881

The result set above contains the necessary data management has deemed necessary to determine the impact each advertisement campaign had on total sales. How are the results read? Row one states $84,122,881 in sales were generated in quarter one and in the following quarter, $81,830,438. As a result of making a change to the historically used advertisement campaign before the start of quarter two, $2,292,443 or 2.8 percent less sales were generated. Row two states $81,830,438 in sales were generated in quarter two and in the following quarter, $82,156,171. As a result of making a change to the historically used advertisement campaign before the start of quarter three, $1,966,710 or 2.39 percent less sales were generated.

Please keep in mind this is a hypothetical scenario and is meant only to demonstrate the use of the LEAD function to solve real-world problems. Determining the efficacy of advertisement campaigns and attributing changes in sales to some event requires more involved analysis and with statistically sound methods.

Knowledge Check
Need more practice or simply wish to test your understanding? Give a few of the practice problems I’ve provided in the SQL Practice Problems section a try.