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_date | product_category | total_sale |
|---|---|---|
| 01-JAN-23 | Software | 130986 |
| 01-JAN-23 | Hardware | 325957 |
| 01-JAN-23 | Office Supplies | 359381 |
| 02-JAN-23 | Software | 350039 |
| 02-JAN-23 | Hardware | 235789 |
| 02-JAN-23 | Office Supplies | 291373 |
| 03-JAN-23 | Software | 463015 |
| 03-JAN-23 | Hardware | 382062 |
| 03-JAN-23 | Office Supplies | 106383 |
| 04-JAN-23 | Software | 499010 |
| 04-JAN-23 | Hardware | 116633 |
| 04-JAN-23 | Office Supplies | 441220 |
| 05-JAN-23 | Software | 482840 |
| 05-JAN-23 | Hardware | 456018 |
| 05-JAN-23 | Office Supplies | 308338 |
| 06-JAN-23 | Software | 463402 |
| 06-JAN-23 | Hardware | 385487 |
| 06-JAN-23 | Office Supplies | 244131 |
| 07-JAN-23 | Software | 247090 |
| 07-JAN-23 | Hardware | 198045 |
| 07-JAN-23 | Office Supplies | 359830 |
| 08-JAN-23 | Software | 372801 |
| 08-JAN-23 | Hardware | 471010 |
| 08-JAN-23 | Office Supplies | 450011 |
| 09-JAN-23 | Software | 401816 |
| 09-JAN-23 | Hardware | 466308 |
| 09-JAN-23 | Office Supplies | 119207 |
| 10-JAN-23 | Software | 146721 |
| 10-JAN-23 | Hardware | 314424 |
| 10-JAN-23 | Office Supplies | 291614 |
| 11-JAN-23 | Software | 184358 |
| 11-JAN-23 | Hardware | 447629 |
| 11-JAN-23 | Office Supplies | 445219 |
| 12-JAN-23 | Software | 122224 |
| 12-JAN-23 | Hardware | 491773 |
| 12-JAN-23 | Office Supplies | 377890 |
| 13-JAN-23 | Software | 216145 |
| 13-JAN-23 | Hardware | 212298 |
| 13-JAN-23 | Office Supplies | 269156 |
| 14-JAN-23 | Software | 117248 |
| 14-JAN-23 | Hardware | 353179 |
| 14-JAN-23 | Office Supplies | 488083 |
| 15-JAN-23 | Software | 130295 |
| 15-JAN-23 | Hardware | 468557 |
| 15-JAN-23 | Office Supplies | 122763 |
| 16-JAN-23 | Software | 295801 |
| 16-JAN-23 | Hardware | 487853 |
| 16-JAN-23 | Office Supplies | 459475 |
| 17-JAN-23 | Software | 184603 |
| 17-JAN-23 | Hardware | 275430 |
| 17-JAN-23 | Office Supplies | 222975 |
| 18-JAN-23 | Software | 421602 |
| 18-JAN-23 | Hardware | 191991 |
| 18-JAN-23 | Office Supplies | 280727 |
| 19-JAN-23 | Software | 388766 |
| 19-JAN-23 | Hardware | 432802 |
| 19-JAN-23 | Office Supplies | 292265 |
| 20-JAN-23 | Software | 378589 |
| 20-JAN-23 | Hardware | 396760 |
| 20-JAN-23 | Office Supplies | 450214 |
| 21-JAN-23 | Software | 482988 |
| 21-JAN-23 | Hardware | 147373 |
| 21-JAN-23 | Office Supplies | 329691 |
| 22-JAN-23 | Software | 451602 |
| 22-JAN-23 | Hardware | 121235 |
| 22-JAN-23 | Office Supplies | 108407 |
| 23-JAN-23 | Software | 319111 |
| 23-JAN-23 | Hardware | 198986 |
| 23-JAN-23 | Office Supplies | 168317 |
| 24-JAN-23 | Software | 477495 |
| 24-JAN-23 | Hardware | 480650 |
| 24-JAN-23 | Office Supplies | 238667 |
| 25-JAN-23 | Software | 232214 |
| 25-JAN-23 | Hardware | 277385 |
| 25-JAN-23 | Office Supplies | 166603 |
| 26-JAN-23 | Software | 382594 |
| 26-JAN-23 | Hardware | 168602 |
| 26-JAN-23 | Office Supplies | 458864 |
| 27-JAN-23 | Software | 409486 |
| 27-JAN-23 | Hardware | 486419 |
| 27-JAN-23 | Office Supplies | 241738 |
| 28-JAN-23 | Software | 242183 |
| 28-JAN-23 | Hardware | 468323 |
| 28-JAN-23 | Office Supplies | 406416 |
| 29-JAN-23 | Software | 472342 |
| 29-JAN-23 | Hardware | 473124 |
| 29-JAN-23 | Office Supplies | 380432 |
| 30-JAN-23 | Software | 237485 |
| 30-JAN-23 | Hardware | 400814 |
| 30-JAN-23 | Office Supplies | 388534 |
| 31-JAN-23 | Software | 328104 |
| 31-JAN-23 | Hardware | 199133 |
| 31-JAN-23 | Office Supplies | 259672 |
| 01-FEB-23 | Software | 498848 |
| 01-FEB-23 | Hardware | 468851 |
| 01-FEB-23 | Office Supplies | 374415 |
| 02-FEB-23 | Software | 496490 |
| 02-FEB-23 | Hardware | 349406 |
| 02-FEB-23 | Office Supplies | 167543 |
| 03-FEB-23 | Software | 191080 |
| 03-FEB-23 | Hardware | 273296 |
| 03-FEB-23 | Office Supplies | 468504 |
| 04-FEB-23 | Software | 198142 |
| 04-FEB-23 | Hardware | 316817 |
| 04-FEB-23 | Office Supplies | 452816 |
| 05-FEB-23 | Software | 289578 |
| 05-FEB-23 | Hardware | 304197 |
| 05-FEB-23 | Office Supplies | 260594 |
| 06-FEB-23 | Software | 239591 |
| 06-FEB-23 | Hardware | 298252 |
| 06-FEB-23 | Office Supplies | 419222 |
| 07-FEB-23 | Software | 434004 |
| 07-FEB-23 | Hardware | 120772 |
| 07-FEB-23 | Office Supplies | 365084 |
| 08-FEB-23 | Software | 356630 |
| 08-FEB-23 | Hardware | 455330 |
| 08-FEB-23 | Office Supplies | 120497 |
| 09-FEB-23 | Software | 210124 |
| 09-FEB-23 | Hardware | 189088 |
| 09-FEB-23 | Office Supplies | 422049 |
| 10-FEB-23 | Software | 396941 |
| 10-FEB-23 | Hardware | 147215 |
| 10-FEB-23 | Office Supplies | 298119 |
| 11-FEB-23 | Software | 327989 |
| 11-FEB-23 | Hardware | 476343 |
| 11-FEB-23 | Office Supplies | 449502 |
| 12-FEB-23 | Software | 113082 |
| 12-FEB-23 | Hardware | 228704 |
| 12-FEB-23 | Office Supplies | 126792 |
| 13-FEB-23 | Software | 234147 |
| 13-FEB-23 | Hardware | 473675 |
| 13-FEB-23 | Office Supplies | 341899 |
| 14-FEB-23 | Software | 427368 |
| 14-FEB-23 | Hardware | 107628 |
| 14-FEB-23 | Office Supplies | 303236 |
| 15-FEB-23 | Software | 194554 |
| 15-FEB-23 | Hardware | 431761 |
| 15-FEB-23 | Office Supplies | 272520 |
| 16-FEB-23 | Software | 167010 |
| 16-FEB-23 | Hardware | 211933 |
| 16-FEB-23 | Office Supplies | 271376 |
| 17-FEB-23 | Software | 299654 |
| 17-FEB-23 | Hardware | 171489 |
| 17-FEB-23 | Office Supplies | 421586 |
| 18-FEB-23 | Software | 253618 |
| 18-FEB-23 | Hardware | 184734 |
| 18-FEB-23 | Office Supplies | 154463 |
| 19-FEB-23 | Software | 280170 |
| 19-FEB-23 | Hardware | 208972 |
| 19-FEB-23 | Office Supplies | 157939 |
| 20-FEB-23 | Software | 296464 |
| 20-FEB-23 | Hardware | 393431 |
| 20-FEB-23 | Office Supplies | 396622 |
| 21-FEB-23 | Software | 365663 |
| 21-FEB-23 | Hardware | 419070 |
| 21-FEB-23 | Office Supplies | 249639 |
| 22-FEB-23 | Software | 418007 |
| 22-FEB-23 | Hardware | 211224 |
| 22-FEB-23 | Office Supplies | 445522 |
| 23-FEB-23 | Software | 479608 |
| 23-FEB-23 | Hardware | 423726 |
| 23-FEB-23 | Office Supplies | 277872 |
| 24-FEB-23 | Software | 102493 |
| 24-FEB-23 | Hardware | 166960 |
| 24-FEB-23 | Office Supplies | 305881 |
| 25-FEB-23 | Software | 222458 |
| 25-FEB-23 | Hardware | 128558 |
| 25-FEB-23 | Office Supplies | 452534 |
| 26-FEB-23 | Software | 137605 |
| 26-FEB-23 | Hardware | 423640 |
| 26-FEB-23 | Office Supplies | 167263 |
| 27-FEB-23 | Software | 291874 |
| 27-FEB-23 | Hardware | 409779 |
| 27-FEB-23 | Office Supplies | 260577 |
| 28-FEB-23 | Software | 343636 |
| 28-FEB-23 | Hardware | 150560 |
| 28-FEB-23 | Office Supplies | 231950 |
| 01-MAR-23 | Software | 352583 |
| 01-MAR-23 | Hardware | 108159 |
| 01-MAR-23 | Office Supplies | 185315 |
| 02-MAR-23 | Software | 107441 |
| 02-MAR-23 | Hardware | 309547 |
| 02-MAR-23 | Office Supplies | 122136 |
| 03-MAR-23 | Software | 423414 |
| 03-MAR-23 | Hardware | 499811 |
| 03-MAR-23 | Office Supplies | 392746 |
| 04-MAR-23 | Software | 491539 |
| 04-MAR-23 | Hardware | 345375 |
| 04-MAR-23 | Office Supplies | 218853 |
| 05-MAR-23 | Software | 248758 |
| 05-MAR-23 | Hardware | 174163 |
| 05-MAR-23 | Office Supplies | 355488 |
| 06-MAR-23 | Software | 450393 |
| 06-MAR-23 | Hardware | 169503 |
| 06-MAR-23 | Office Supplies | 278893 |
| 07-MAR-23 | Software | 477258 |
| 07-MAR-23 | Hardware | 422113 |
| 07-MAR-23 | Office Supplies | 200048 |
| 08-MAR-23 | Software | 374121 |
| 08-MAR-23 | Hardware | 291224 |
| 08-MAR-23 | Office Supplies | 108375 |
| 09-MAR-23 | Software | 221727 |
| 09-MAR-23 | Hardware | 472045 |
| 09-MAR-23 | Office Supplies | 118751 |
| 10-MAR-23 | Software | 318517 |
| 10-MAR-23 | Hardware | 140101 |
| 10-MAR-23 | Office Supplies | 443992 |
| 11-MAR-23 | Software | 128573 |
| 11-MAR-23 | Hardware | 218247 |
| 11-MAR-23 | Office Supplies | 427230 |
| 12-MAR-23 | Software | 334955 |
| 12-MAR-23 | Hardware | 425448 |
| 12-MAR-23 | Office Supplies | 420772 |
| 13-MAR-23 | Software | 467659 |
| 13-MAR-23 | Hardware | 403023 |
| 13-MAR-23 | Office Supplies | 423537 |
| 14-MAR-23 | Software | 170618 |
| 14-MAR-23 | Hardware | 494032 |
| 14-MAR-23 | Office Supplies | 412336 |
| 15-MAR-23 | Software | 424735 |
| 15-MAR-23 | Hardware | 371217 |
| 15-MAR-23 | Office Supplies | 202721 |
| 16-MAR-23 | Software | 308023 |
| 16-MAR-23 | Hardware | 487999 |
| 16-MAR-23 | Office Supplies | 493143 |
| 17-MAR-23 | Software | 346157 |
| 17-MAR-23 | Hardware | 269133 |
| 17-MAR-23 | Office Supplies | 331892 |
| 18-MAR-23 | Software | 360626 |
| 18-MAR-23 | Hardware | 217758 |
| 18-MAR-23 | Office Supplies | 341784 |
| 19-MAR-23 | Software | 240326 |
| 19-MAR-23 | Hardware | 474311 |
| 19-MAR-23 | Office Supplies | 480205 |
| 20-MAR-23 | Software | 204067 |
| 20-MAR-23 | Hardware | 325958 |
| 20-MAR-23 | Office Supplies | 349648 |
| 21-MAR-23 | Software | 466128 |
| 21-MAR-23 | Hardware | 451988 |
| 21-MAR-23 | Office Supplies | 218058 |
| 22-MAR-23 | Software | 319976 |
| 22-MAR-23 | Hardware | 326494 |
| 22-MAR-23 | Office Supplies | 270824 |
| 23-MAR-23 | Software | 139625 |
| 23-MAR-23 | Hardware | 216417 |
| 23-MAR-23 | Office Supplies | 477186 |
| 24-MAR-23 | Software | 279025 |
| 24-MAR-23 | Hardware | 121012 |
| 24-MAR-23 | Office Supplies | 163536 |
| 25-MAR-23 | Software | 191230 |
| 25-MAR-23 | Hardware | 401993 |
| 25-MAR-23 | Office Supplies | 293330 |
| 26-MAR-23 | Software | 302770 |
| 26-MAR-23 | Hardware | 182144 |
| 26-MAR-23 | Office Supplies | 279223 |
| 27-MAR-23 | Software | 101519 |
| 27-MAR-23 | Hardware | 467885 |
| 27-MAR-23 | Office Supplies | 241178 |
| 28-MAR-23 | Software | 350644 |
| 28-MAR-23 | Hardware | 179143 |
| 28-MAR-23 | Office Supplies | 257859 |
| 29-MAR-23 | Software | 285776 |
| 29-MAR-23 | Hardware | 268899 |
| 29-MAR-23 | Office Supplies | 192559 |
| 30-MAR-23 | Software | 407435 |
| 30-MAR-23 | Hardware | 439023 |
| 30-MAR-23 | Office Supplies | 251407 |
| 31-MAR-23 | Software | 270420 |
| 31-MAR-23 | Hardware | 433787 |
| 31-MAR-23 | Office Supplies | 355011 |
| 01-APR-23 | Software | 141595 |
| 01-APR-23 | Hardware | 390594 |
| 01-APR-23 | Office Supplies | 320527 |
| 02-APR-23 | Software | 183160 |
| 02-APR-23 | Hardware | 464361 |
| 02-APR-23 | Office Supplies | 487635 |
| 03-APR-23 | Software | 179908 |
| 03-APR-23 | Hardware | 487048 |
| 03-APR-23 | Office Supplies | 109011 |
| 04-APR-23 | Software | 156679 |
| 04-APR-23 | Hardware | 437387 |
| 04-APR-23 | Office Supplies | 171126 |
| 05-APR-23 | Software | 125223 |
| 05-APR-23 | Hardware | 163396 |
| 05-APR-23 | Office Supplies | 299903 |
| 06-APR-23 | Software | 121008 |
| 06-APR-23 | Hardware | 241845 |
| 06-APR-23 | Office Supplies | 442197 |
| 07-APR-23 | Software | 221383 |
| 07-APR-23 | Hardware | 454711 |
| 07-APR-23 | Office Supplies | 405102 |
| 08-APR-23 | Software | 107507 |
| 08-APR-23 | Hardware | 251905 |
| 08-APR-23 | Office Supplies | 220887 |
| 09-APR-23 | Software | 310617 |
| 09-APR-23 | Hardware | 227411 |
| 09-APR-23 | Office Supplies | 265518 |
| 10-APR-23 | Software | 422232 |
| 10-APR-23 | Hardware | 310046 |
| 10-APR-23 | Office Supplies | 150205 |
| 11-APR-23 | Software | 332198 |
| 11-APR-23 | Hardware | 320621 |
| 11-APR-23 | Office Supplies | 411607 |
| 12-APR-23 | Software | 384063 |
| 12-APR-23 | Hardware | 274390 |
| 12-APR-23 | Office Supplies | 366354 |
| 13-APR-23 | Software | 280966 |
| 13-APR-23 | Hardware | 382678 |
| 13-APR-23 | Office Supplies | 169193 |
| 14-APR-23 | Software | 288234 |
| 14-APR-23 | Hardware | 158199 |
| 14-APR-23 | Office Supplies | 405273 |
| 15-APR-23 | Software | 312305 |
| 15-APR-23 | Hardware | 375867 |
| 15-APR-23 | Office Supplies | 242540 |
| 16-APR-23 | Software | 457762 |
| 16-APR-23 | Hardware | 306784 |
| 16-APR-23 | Office Supplies | 410744 |
| 17-APR-23 | Software | 134756 |
| 17-APR-23 | Hardware | 128819 |
| 17-APR-23 | Office Supplies | 393735 |
| 18-APR-23 | Software | 156509 |
| 18-APR-23 | Hardware | 277928 |
| 18-APR-23 | Office Supplies | 185693 |
| 19-APR-23 | Software | 475899 |
| 19-APR-23 | Hardware | 352213 |
| 19-APR-23 | Office Supplies | 118006 |
| 20-APR-23 | Software | 486045 |
| 20-APR-23 | Hardware | 105392 |
| 20-APR-23 | Office Supplies | 274407 |
| 21-APR-23 | Software | 137841 |
| 21-APR-23 | Hardware | 412107 |
| 21-APR-23 | Office Supplies | 307669 |
| 22-APR-23 | Software | 420618 |
| 22-APR-23 | Hardware | 440743 |
| 22-APR-23 | Office Supplies | 211778 |
| 23-APR-23 | Software | 437481 |
| 23-APR-23 | Hardware | 306189 |
| 23-APR-23 | Office Supplies | 446075 |
| 24-APR-23 | Software | 369096 |
| 24-APR-23 | Hardware | 309242 |
| 24-APR-23 | Office Supplies | 300044 |
| 25-APR-23 | Software | 347470 |
| 25-APR-23 | Hardware | 433689 |
| 25-APR-23 | Office Supplies | 330579 |
| 26-APR-23 | Software | 147642 |
| 26-APR-23 | Hardware | 465270 |
| 26-APR-23 | Office Supplies | 458689 |
| 27-APR-23 | Software | 131632 |
| 27-APR-23 | Hardware | 345373 |
| 27-APR-23 | Office Supplies | 256230 |
| 28-APR-23 | Software | 159253 |
| 28-APR-23 | Hardware | 478742 |
| 28-APR-23 | Office Supplies | 487975 |
| 29-APR-23 | Software | 235898 |
| 29-APR-23 | Hardware | 308098 |
| 29-APR-23 | Office Supplies | 172834 |
| 30-APR-23 | Software | 429613 |
| 30-APR-23 | Hardware | 370109 |
| 30-APR-23 | Office Supplies | 279783 |
| 01-MAY-23 | Software | 140762 |
| 01-MAY-23 | Hardware | 318807 |
| 01-MAY-23 | Office Supplies | 294785 |
| 02-MAY-23 | Software | 376862 |
| 02-MAY-23 | Hardware | 208853 |
| 02-MAY-23 | Office Supplies | 498942 |
| 03-MAY-23 | Software | 117052 |
| 03-MAY-23 | Hardware | 249786 |
| 03-MAY-23 | Office Supplies | 182057 |
| 04-MAY-23 | Software | 188616 |
| 04-MAY-23 | Hardware | 326858 |
| 04-MAY-23 | Office Supplies | 116027 |
| 05-MAY-23 | Software | 210789 |
| 05-MAY-23 | Hardware | 382226 |
| 05-MAY-23 | Office Supplies | 462508 |
| 06-MAY-23 | Software | 224315 |
| 06-MAY-23 | Hardware | 204152 |
| 06-MAY-23 | Office Supplies | 243199 |
| 07-MAY-23 | Software | 144383 |
| 07-MAY-23 | Hardware | 107532 |
| 07-MAY-23 | Office Supplies | 197586 |
| 08-MAY-23 | Software | 274237 |
| 08-MAY-23 | Hardware | 145299 |
| 08-MAY-23 | Office Supplies | 484134 |
| 09-MAY-23 | Software | 262382 |
| 09-MAY-23 | Hardware | 273740 |
| 09-MAY-23 | Office Supplies | 220205 |
| 10-MAY-23 | Software | 380503 |
| 10-MAY-23 | Hardware | 350232 |
| 10-MAY-23 | Office Supplies | 310852 |
| 11-MAY-23 | Software | 391562 |
| 11-MAY-23 | Hardware | 478243 |
| 11-MAY-23 | Office Supplies | 124996 |
| 12-MAY-23 | Software | 240861 |
| 12-MAY-23 | Hardware | 245958 |
| 12-MAY-23 | Office Supplies | 418096 |
| 13-MAY-23 | Software | 298986 |
| 13-MAY-23 | Hardware | 364523 |
| 13-MAY-23 | Office Supplies | 183475 |
| 14-MAY-23 | Software | 412636 |
| 14-MAY-23 | Hardware | 236258 |
| 14-MAY-23 | Office Supplies | 292129 |
| 15-MAY-23 | Software | 474717 |
| 15-MAY-23 | Hardware | 242421 |
| 15-MAY-23 | Office Supplies | 227600 |
| 16-MAY-23 | Software | 218739 |
| 16-MAY-23 | Hardware | 283568 |
| 16-MAY-23 | Office Supplies | 182894 |
| 17-MAY-23 | Software | 231174 |
| 17-MAY-23 | Hardware | 280282 |
| 17-MAY-23 | Office Supplies | 315630 |
| 18-MAY-23 | Software | 270421 |
| 18-MAY-23 | Hardware | 203850 |
| 18-MAY-23 | Office Supplies | 415408 |
| 19-MAY-23 | Software | 263918 |
| 19-MAY-23 | Hardware | 303145 |
| 19-MAY-23 | Office Supplies | 492547 |
| 20-MAY-23 | Software | 414991 |
| 20-MAY-23 | Hardware | 257366 |
| 20-MAY-23 | Office Supplies | 459085 |
| 21-MAY-23 | Software | 309794 |
| 21-MAY-23 | Hardware | 408615 |
| 21-MAY-23 | Office Supplies | 228029 |
| 22-MAY-23 | Software | 207054 |
| 22-MAY-23 | Hardware | 329477 |
| 22-MAY-23 | Office Supplies | 472817 |
| 23-MAY-23 | Software | 434124 |
| 23-MAY-23 | Hardware | 409775 |
| 23-MAY-23 | Office Supplies | 246750 |
| 24-MAY-23 | Software | 145983 |
| 24-MAY-23 | Hardware | 136951 |
| 24-MAY-23 | Office Supplies | 340411 |
| 25-MAY-23 | Software | 435328 |
| 25-MAY-23 | Hardware | 119100 |
| 25-MAY-23 | Office Supplies | 249954 |
| 26-MAY-23 | Software | 325187 |
| 26-MAY-23 | Hardware | 392976 |
| 26-MAY-23 | Office Supplies | 328867 |
| 27-MAY-23 | Software | 167029 |
| 27-MAY-23 | Hardware | 393557 |
| 27-MAY-23 | Office Supplies | 454022 |
| 28-MAY-23 | Software | 435836 |
| 28-MAY-23 | Hardware | 150924 |
| 28-MAY-23 | Office Supplies | 454083 |
| 29-MAY-23 | Software | 226260 |
| 29-MAY-23 | Hardware | 155481 |
| 29-MAY-23 | Office Supplies | 281388 |
| 30-MAY-23 | Software | 117544 |
| 30-MAY-23 | Hardware | 155852 |
| 30-MAY-23 | Office Supplies | 403325 |
| 31-MAY-23 | Software | 377182 |
| 31-MAY-23 | Hardware | 108781 |
| 31-MAY-23 | Office Supplies | 273138 |
| 01-JUN-23 | Software | 311505 |
| 01-JUN-23 | Hardware | 119690 |
| 01-JUN-23 | Office Supplies | 465736 |
| 02-JUN-23 | Software | 264947 |
| 02-JUN-23 | Hardware | 408841 |
| 02-JUN-23 | Office Supplies | 152197 |
| 03-JUN-23 | Software | 374350 |
| 03-JUN-23 | Hardware | 264722 |
| 03-JUN-23 | Office Supplies | 320520 |
| 04-JUN-23 | Software | 423306 |
| 04-JUN-23 | Hardware | 166503 |
| 04-JUN-23 | Office Supplies | 299382 |
| 05-JUN-23 | Software | 465584 |
| 05-JUN-23 | Hardware | 495608 |
| 05-JUN-23 | Office Supplies | 496827 |
| 06-JUN-23 | Software | 244276 |
| 06-JUN-23 | Hardware | 330948 |
| 06-JUN-23 | Office Supplies | 196702 |
| 07-JUN-23 | Software | 446569 |
| 07-JUN-23 | Hardware | 350827 |
| 07-JUN-23 | Office Supplies | 308290 |
| 08-JUN-23 | Software | 413169 |
| 08-JUN-23 | Hardware | 436055 |
| 08-JUN-23 | Office Supplies | 464097 |
| 09-JUN-23 | Software | 409417 |
| 09-JUN-23 | Hardware | 224598 |
| 09-JUN-23 | Office Supplies | 385330 |
| 10-JUN-23 | Software | 376142 |
| 10-JUN-23 | Hardware | 311306 |
| 10-JUN-23 | Office Supplies | 418557 |
| 11-JUN-23 | Software | 419889 |
| 11-JUN-23 | Hardware | 357489 |
| 11-JUN-23 | Office Supplies | 156642 |
| 12-JUN-23 | Software | 306148 |
| 12-JUN-23 | Hardware | 200276 |
| 12-JUN-23 | Office Supplies | 427942 |
| 13-JUN-23 | Software | 302151 |
| 13-JUN-23 | Hardware | 199538 |
| 13-JUN-23 | Office Supplies | 157699 |
| 14-JUN-23 | Software | 149388 |
| 14-JUN-23 | Hardware | 490336 |
| 14-JUN-23 | Office Supplies | 460060 |
| 15-JUN-23 | Software | 253405 |
| 15-JUN-23 | Hardware | 401420 |
| 15-JUN-23 | Office Supplies | 146533 |
| 16-JUN-23 | Software | 450910 |
| 16-JUN-23 | Hardware | 370536 |
| 16-JUN-23 | Office Supplies | 386430 |
| 17-JUN-23 | Software | 378090 |
| 17-JUN-23 | Hardware | 464114 |
| 17-JUN-23 | Office Supplies | 406680 |
| 18-JUN-23 | Software | 211615 |
| 18-JUN-23 | Hardware | 290351 |
| 18-JUN-23 | Office Supplies | 444837 |
| 19-JUN-23 | Software | 237235 |
| 19-JUN-23 | Hardware | 220923 |
| 19-JUN-23 | Office Supplies | 244289 |
| 20-JUN-23 | Software | 351066 |
| 20-JUN-23 | Hardware | 141090 |
| 20-JUN-23 | Office Supplies | 220148 |
| 21-JUN-23 | Software | 470754 |
| 21-JUN-23 | Hardware | 294240 |
| 21-JUN-23 | Office Supplies | 122212 |
| 22-JUN-23 | Software | 377163 |
| 22-JUN-23 | Hardware | 229455 |
| 22-JUN-23 | Office Supplies | 266779 |
| 23-JUN-23 | Software | 227325 |
| 23-JUN-23 | Hardware | 267736 |
| 23-JUN-23 | Office Supplies | 195147 |
| 24-JUN-23 | Software | 154526 |
| 24-JUN-23 | Hardware | 293664 |
| 24-JUN-23 | Office Supplies | 321285 |
| 25-JUN-23 | Software | 156763 |
| 25-JUN-23 | Hardware | 199975 |
| 25-JUN-23 | Office Supplies | 252248 |
| 26-JUN-23 | Software | 354824 |
| 26-JUN-23 | Hardware | 364079 |
| 26-JUN-23 | Office Supplies | 306592 |
| 27-JUN-23 | Software | 350527 |
| 27-JUN-23 | Hardware | 287508 |
| 27-JUN-23 | Office Supplies | 188713 |
| 28-JUN-23 | Software | 292010 |
| 28-JUN-23 | Hardware | 487758 |
| 28-JUN-23 | Office Supplies | 101658 |
| 29-JUN-23 | Software | 363394 |
| 29-JUN-23 | Hardware | 157124 |
| 29-JUN-23 | Office Supplies | 478412 |
| 30-JUN-23 | Software | 300932 |
| 30-JUN-23 | Hardware | 157215 |
| 30-JUN-23 | Office Supplies | 241366 |
| 01-JUL-23 | Software | 148090 |
| 01-JUL-23 | Hardware | 272906 |
| 01-JUL-23 | Office Supplies | 393778 |
| 02-JUL-23 | Software | 179911 |
| 02-JUL-23 | Hardware | 426551 |
| 02-JUL-23 | Office Supplies | 219791 |
| 03-JUL-23 | Software | 226839 |
| 03-JUL-23 | Hardware | 380730 |
| 03-JUL-23 | Office Supplies | 169157 |
| 04-JUL-23 | Software | 241680 |
| 04-JUL-23 | Hardware | 105436 |
| 04-JUL-23 | Office Supplies | 164201 |
| 05-JUL-23 | Software | 207715 |
| 05-JUL-23 | Hardware | 434853 |
| 05-JUL-23 | Office Supplies | 164089 |
| 06-JUL-23 | Software | 158928 |
| 06-JUL-23 | Hardware | 466440 |
| 06-JUL-23 | Office Supplies | 154431 |
| 07-JUL-23 | Software | 251429 |
| 07-JUL-23 | Hardware | 487763 |
| 07-JUL-23 | Office Supplies | 408431 |
| 08-JUL-23 | Software | 333003 |
| 08-JUL-23 | Hardware | 143077 |
| 08-JUL-23 | Office Supplies | 128848 |
| 09-JUL-23 | Software | 221806 |
| 09-JUL-23 | Hardware | 334149 |
| 09-JUL-23 | Office Supplies | 351364 |
| 10-JUL-23 | Software | 100624 |
| 10-JUL-23 | Hardware | 178095 |
| 10-JUL-23 | Office Supplies | 286670 |
| 11-JUL-23 | Software | 408145 |
| 11-JUL-23 | Hardware | 275415 |
| 11-JUL-23 | Office Supplies | 440642 |
| 12-JUL-23 | Software | 488925 |
| 12-JUL-23 | Hardware | 234437 |
| 12-JUL-23 | Office Supplies | 220215 |
| 13-JUL-23 | Software | 441077 |
| 13-JUL-23 | Hardware | 283602 |
| 13-JUL-23 | Office Supplies | 480705 |
| 14-JUL-23 | Software | 321405 |
| 14-JUL-23 | Hardware | 496504 |
| 14-JUL-23 | Office Supplies | 369516 |
| 15-JUL-23 | Software | 370793 |
| 15-JUL-23 | Hardware | 458243 |
| 15-JUL-23 | Office Supplies | 222362 |
| 16-JUL-23 | Software | 252803 |
| 16-JUL-23 | Hardware | 350939 |
| 16-JUL-23 | Office Supplies | 454198 |
| 17-JUL-23 | Software | 156089 |
| 17-JUL-23 | Hardware | 114823 |
| 17-JUL-23 | Office Supplies | 144888 |
| 18-JUL-23 | Software | 386844 |
| 18-JUL-23 | Hardware | 133935 |
| 18-JUL-23 | Office Supplies | 200292 |
| 19-JUL-23 | Software | 270214 |
| 19-JUL-23 | Hardware | 269897 |
| 19-JUL-23 | Office Supplies | 107055 |
| 20-JUL-23 | Software | 245143 |
| 20-JUL-23 | Hardware | 180536 |
| 20-JUL-23 | Office Supplies | 104647 |
| 21-JUL-23 | Software | 406462 |
| 21-JUL-23 | Hardware | 134985 |
| 21-JUL-23 | Office Supplies | 156146 |
| 22-JUL-23 | Software | 109799 |
| 22-JUL-23 | Hardware | 230605 |
| 22-JUL-23 | Office Supplies | 239874 |
| 23-JUL-23 | Software | 284416 |
| 23-JUL-23 | Hardware | 148793 |
| 23-JUL-23 | Office Supplies | 218456 |
| 24-JUL-23 | Software | 144795 |
| 24-JUL-23 | Hardware | 380334 |
| 24-JUL-23 | Office Supplies | 462944 |
| 25-JUL-23 | Software | 423947 |
| 25-JUL-23 | Hardware | 122222 |
| 25-JUL-23 | Office Supplies | 446006 |
| 26-JUL-23 | Software | 130794 |
| 26-JUL-23 | Hardware | 485806 |
| 26-JUL-23 | Office Supplies | 394017 |
| 27-JUL-23 | Software | 483047 |
| 27-JUL-23 | Hardware | 277896 |
| 27-JUL-23 | Office Supplies | 348972 |
| 28-JUL-23 | Software | 396253 |
| 28-JUL-23 | Hardware | 387469 |
| 28-JUL-23 | Office Supplies | 212031 |
| 29-JUL-23 | Software | 386963 |
| 29-JUL-23 | Hardware | 178360 |
| 29-JUL-23 | Office Supplies | 445313 |
| 30-JUL-23 | Software | 447697 |
| 30-JUL-23 | Hardware | 234068 |
| 30-JUL-23 | Office Supplies | 314974 |
| 31-JUL-23 | Software | 224862 |
| 31-JUL-23 | Hardware | 347539 |
| 31-JUL-23 | Office Supplies | 318588 |
| 01-AUG-23 | Software | 136851 |
| 01-AUG-23 | Hardware | 331205 |
| 01-AUG-23 | Office Supplies | 227110 |
| 02-AUG-23 | Software | 109391 |
| 02-AUG-23 | Hardware | 155210 |
| 02-AUG-23 | Office Supplies | 107036 |
| 03-AUG-23 | Software | 340818 |
| 03-AUG-23 | Hardware | 297598 |
| 03-AUG-23 | Office Supplies | 231274 |
| 04-AUG-23 | Software | 417143 |
| 04-AUG-23 | Hardware | 480037 |
| 04-AUG-23 | Office Supplies | 137046 |
| 05-AUG-23 | Software | 490894 |
| 05-AUG-23 | Hardware | 417638 |
| 05-AUG-23 | Office Supplies | 119741 |
| 06-AUG-23 | Software | 494310 |
| 06-AUG-23 | Hardware | 253262 |
| 06-AUG-23 | Office Supplies | 447793 |
| 07-AUG-23 | Software | 356028 |
| 07-AUG-23 | Hardware | 141396 |
| 07-AUG-23 | Office Supplies | 468005 |
| 08-AUG-23 | Software | 216678 |
| 08-AUG-23 | Hardware | 293425 |
| 08-AUG-23 | Office Supplies | 213345 |
| 09-AUG-23 | Software | 101459 |
| 09-AUG-23 | Hardware | 457496 |
| 09-AUG-23 | Office Supplies | 364674 |
| 10-AUG-23 | Software | 454849 |
| 10-AUG-23 | Hardware | 409279 |
| 10-AUG-23 | Office Supplies | 396333 |
| 11-AUG-23 | Software | 437044 |
| 11-AUG-23 | Hardware | 181646 |
| 11-AUG-23 | Office Supplies | 211539 |
| 12-AUG-23 | Software | 190055 |
| 12-AUG-23 | Hardware | 433339 |
| 12-AUG-23 | Office Supplies | 177433 |
| 13-AUG-23 | Software | 453043 |
| 13-AUG-23 | Hardware | 371613 |
| 13-AUG-23 | Office Supplies | 283404 |
| 14-AUG-23 | Software | 125292 |
| 14-AUG-23 | Hardware | 400191 |
| 14-AUG-23 | Office Supplies | 257934 |
| 15-AUG-23 | Software | 386019 |
| 15-AUG-23 | Hardware | 387147 |
| 15-AUG-23 | Office Supplies | 305108 |
| 16-AUG-23 | Software | 231773 |
| 16-AUG-23 | Hardware | 381273 |
| 16-AUG-23 | Office Supplies | 331623 |
| 17-AUG-23 | Software | 393107 |
| 17-AUG-23 | Hardware | 303725 |
| 17-AUG-23 | Office Supplies | 275465 |
| 18-AUG-23 | Software | 282980 |
| 18-AUG-23 | Hardware | 341541 |
| 18-AUG-23 | Office Supplies | 140965 |
| 19-AUG-23 | Software | 431933 |
| 19-AUG-23 | Hardware | 138311 |
| 19-AUG-23 | Office Supplies | 288702 |
| 20-AUG-23 | Software | 491784 |
| 20-AUG-23 | Hardware | 464240 |
| 20-AUG-23 | Office Supplies | 464490 |
| 21-AUG-23 | Software | 403369 |
| 21-AUG-23 | Hardware | 277863 |
| 21-AUG-23 | Office Supplies | 379244 |
| 22-AUG-23 | Software | 342814 |
| 22-AUG-23 | Hardware | 107198 |
| 22-AUG-23 | Office Supplies | 413377 |
| 23-AUG-23 | Software | 214480 |
| 23-AUG-23 | Hardware | 443937 |
| 23-AUG-23 | Office Supplies | 289252 |
| 24-AUG-23 | Software | 303146 |
| 24-AUG-23 | Hardware | 119602 |
| 24-AUG-23 | Office Supplies | 153454 |
| 25-AUG-23 | Software | 205728 |
| 25-AUG-23 | Hardware | 242047 |
| 25-AUG-23 | Office Supplies | 428543 |
| 26-AUG-23 | Software | 273114 |
| 26-AUG-23 | Hardware | 348451 |
| 26-AUG-23 | Office Supplies | 174699 |
| 27-AUG-23 | Software | 444968 |
| 27-AUG-23 | Hardware | 394566 |
| 27-AUG-23 | Office Supplies | 261222 |
| 28-AUG-23 | Software | 140139 |
| 28-AUG-23 | Hardware | 237829 |
| 28-AUG-23 | Office Supplies | 250821 |
| 29-AUG-23 | Software | 437298 |
| 29-AUG-23 | Hardware | 368978 |
| 29-AUG-23 | Office Supplies | 219958 |
| 30-AUG-23 | Software | 400241 |
| 30-AUG-23 | Hardware | 181840 |
| 30-AUG-23 | Office Supplies | 397579 |
| 31-AUG-23 | Software | 141923 |
| 31-AUG-23 | Hardware | 356412 |
| 31-AUG-23 | Office Supplies | 149477 |
| 01-SEP-23 | Software | 162649 |
| 01-SEP-23 | Hardware | 368106 |
| 01-SEP-23 | Office Supplies | 407390 |
| 02-SEP-23 | Software | 171311 |
| 02-SEP-23 | Hardware | 100499 |
| 02-SEP-23 | Office Supplies | 331085 |
| 03-SEP-23 | Software | 494360 |
| 03-SEP-23 | Hardware | 434919 |
| 03-SEP-23 | Office Supplies | 400876 |
| 04-SEP-23 | Software | 385077 |
| 04-SEP-23 | Hardware | 498835 |
| 04-SEP-23 | Office Supplies | 445773 |
| 05-SEP-23 | Software | 204009 |
| 05-SEP-23 | Hardware | 456094 |
| 05-SEP-23 | Office Supplies | 189993 |
| 06-SEP-23 | Software | 215665 |
| 06-SEP-23 | Hardware | 376902 |
| 06-SEP-23 | Office Supplies | 432878 |
| 07-SEP-23 | Software | 449924 |
| 07-SEP-23 | Hardware | 131924 |
| 07-SEP-23 | Office Supplies | 202070 |
| 08-SEP-23 | Software | 215311 |
| 08-SEP-23 | Hardware | 340668 |
| 08-SEP-23 | Office Supplies | 146842 |
| 09-SEP-23 | Software | 499203 |
| 09-SEP-23 | Hardware | 243056 |
| 09-SEP-23 | Office Supplies | 189038 |
| 10-SEP-23 | Software | 310956 |
| 10-SEP-23 | Hardware | 256403 |
| 10-SEP-23 | Office Supplies | 300350 |
| 11-SEP-23 | Software | 338729 |
| 11-SEP-23 | Hardware | 365795 |
| 11-SEP-23 | Office Supplies | 387708 |
| 12-SEP-23 | Software | 460844 |
| 12-SEP-23 | Hardware | 277039 |
| 12-SEP-23 | Office Supplies | 242547 |
| 13-SEP-23 | Software | 259629 |
| 13-SEP-23 | Hardware | 267474 |
| 13-SEP-23 | Office Supplies | 283371 |
| 14-SEP-23 | Software | 475576 |
| 14-SEP-23 | Hardware | 330046 |
| 14-SEP-23 | Office Supplies | 393402 |
| 15-SEP-23 | Software | 206995 |
| 15-SEP-23 | Hardware | 244148 |
| 15-SEP-23 | Office Supplies | 193924 |
| 16-SEP-23 | Software | 340814 |
| 16-SEP-23 | Hardware | 152964 |
| 16-SEP-23 | Office Supplies | 245881 |
| 17-SEP-23 | Software | 152836 |
| 17-SEP-23 | Hardware | 350166 |
| 17-SEP-23 | Office Supplies | 213764 |
| 18-SEP-23 | Software | 499650 |
| 18-SEP-23 | Hardware | 257235 |
| 18-SEP-23 | Office Supplies | 197081 |
| 19-SEP-23 | Software | 196319 |
| 19-SEP-23 | Hardware | 161852 |
| 19-SEP-23 | Office Supplies | 111162 |
| 20-SEP-23 | Software | 496428 |
| 20-SEP-23 | Hardware | 382268 |
| 20-SEP-23 | Office Supplies | 256903 |
| 21-SEP-23 | Software | 131435 |
| 21-SEP-23 | Hardware | 333089 |
| 21-SEP-23 | Office Supplies | 300714 |
| 22-SEP-23 | Software | 288585 |
| 22-SEP-23 | Hardware | 345921 |
| 22-SEP-23 | Office Supplies | 275874 |
| 23-SEP-23 | Software | 188320 |
| 23-SEP-23 | Hardware | 363638 |
| 23-SEP-23 | Office Supplies | 223569 |
| 24-SEP-23 | Software | 373364 |
| 24-SEP-23 | Hardware | 191241 |
| 24-SEP-23 | Office Supplies | 206948 |
| 25-SEP-23 | Software | 326280 |
| 25-SEP-23 | Hardware | 156920 |
| 25-SEP-23 | Office Supplies | 276073 |
| 26-SEP-23 | Software | 295994 |
| 26-SEP-23 | Hardware | 456126 |
| 26-SEP-23 | Office Supplies | 393633 |
| 27-SEP-23 | Software | 292724 |
| 27-SEP-23 | Hardware | 152039 |
| 27-SEP-23 | Office Supplies | 493222 |
| 28-SEP-23 | Software | 302803 |
| 28-SEP-23 | Hardware | 310606 |
| 28-SEP-23 | Office Supplies | 413639 |
| 29-SEP-23 | Software | 397432 |
| 29-SEP-23 | Hardware | 435049 |
| 29-SEP-23 | Office Supplies | 427647 |
| 30-SEP-23 | Software | 398871 |
| 30-SEP-23 | Hardware | 457273 |
| 30-SEP-23 | Office Supplies | 159307 |
| 01-OCT-23 | Software | 399450 |
| 01-OCT-23 | Hardware | 291064 |
| 01-OCT-23 | Office Supplies | 100564 |
| 02-OCT-23 | Software | 484086 |
| 02-OCT-23 | Hardware | 264161 |
| 02-OCT-23 | Office Supplies | 175967 |
| 03-OCT-23 | Software | 169276 |
| 03-OCT-23 | Hardware | 295316 |
| 03-OCT-23 | Office Supplies | 107787 |
| 04-OCT-23 | Software | 317494 |
| 04-OCT-23 | Hardware | 214179 |
| 04-OCT-23 | Office Supplies | 244205 |
| 05-OCT-23 | Software | 352830 |
| 05-OCT-23 | Hardware | 379116 |
| 05-OCT-23 | Office Supplies | 407086 |
| 06-OCT-23 | Software | 389838 |
| 06-OCT-23 | Hardware | 295644 |
| 06-OCT-23 | Office Supplies | 213362 |
| 07-OCT-23 | Software | 490714 |
| 07-OCT-23 | Hardware | 389043 |
| 07-OCT-23 | Office Supplies | 213891 |
| 08-OCT-23 | Software | 104384 |
| 08-OCT-23 | Hardware | 299232 |
| 08-OCT-23 | Office Supplies | 192875 |
| 09-OCT-23 | Software | 170542 |
| 09-OCT-23 | Hardware | 428867 |
| 09-OCT-23 | Office Supplies | 268138 |
| 10-OCT-23 | Software | 228362 |
| 10-OCT-23 | Hardware | 187456 |
| 10-OCT-23 | Office Supplies | 303195 |
| 11-OCT-23 | Software | 335181 |
| 11-OCT-23 | Hardware | 487771 |
| 11-OCT-23 | Office Supplies | 154703 |
| 12-OCT-23 | Software | 224134 |
| 12-OCT-23 | Hardware | 357450 |
| 12-OCT-23 | Office Supplies | 355402 |
| 13-OCT-23 | Software | 282916 |
| 13-OCT-23 | Hardware | 395557 |
| 13-OCT-23 | Office Supplies | 352236 |
| 14-OCT-23 | Software | 283861 |
| 14-OCT-23 | Hardware | 113488 |
| 14-OCT-23 | Office Supplies | 170306 |
| 15-OCT-23 | Software | 137838 |
| 15-OCT-23 | Hardware | 145554 |
| 15-OCT-23 | Office Supplies | 431156 |
| 16-OCT-23 | Software | 400308 |
| 16-OCT-23 | Hardware | 192641 |
| 16-OCT-23 | Office Supplies | 106251 |
| 17-OCT-23 | Software | 127001 |
| 17-OCT-23 | Hardware | 388146 |
| 17-OCT-23 | Office Supplies | 324092 |
| 18-OCT-23 | Software | 141539 |
| 18-OCT-23 | Hardware | 403256 |
| 18-OCT-23 | Office Supplies | 256505 |
| 19-OCT-23 | Software | 252182 |
| 19-OCT-23 | Hardware | 469993 |
| 19-OCT-23 | Office Supplies | 219932 |
| 20-OCT-23 | Software | 268703 |
| 20-OCT-23 | Hardware | 212448 |
| 20-OCT-23 | Office Supplies | 351617 |
| 21-OCT-23 | Software | 379162 |
| 21-OCT-23 | Hardware | 404458 |
| 21-OCT-23 | Office Supplies | 283087 |
| 22-OCT-23 | Software | 162490 |
| 22-OCT-23 | Hardware | 441628 |
| 22-OCT-23 | Office Supplies | 471630 |
| 23-OCT-23 | Software | 499608 |
| 23-OCT-23 | Hardware | 135746 |
| 23-OCT-23 | Office Supplies | 274674 |
| 24-OCT-23 | Software | 259322 |
| 24-OCT-23 | Hardware | 173699 |
| 24-OCT-23 | Office Supplies | 309133 |
| 25-OCT-23 | Software | 283668 |
| 25-OCT-23 | Hardware | 128553 |
| 25-OCT-23 | Office Supplies | 434598 |
| 26-OCT-23 | Software | 145047 |
| 26-OCT-23 | Hardware | 404693 |
| 26-OCT-23 | Office Supplies | 391874 |
| 27-OCT-23 | Software | 199126 |
| 27-OCT-23 | Hardware | 197544 |
| 27-OCT-23 | Office Supplies | 317014 |
| 28-OCT-23 | Software | 492231 |
| 28-OCT-23 | Hardware | 269902 |
| 28-OCT-23 | Office Supplies | 490712 |
| 29-OCT-23 | Software | 459700 |
| 29-OCT-23 | Hardware | 487364 |
| 29-OCT-23 | Office Supplies | 457764 |
| 30-OCT-23 | Software | 274636 |
| 30-OCT-23 | Hardware | 392838 |
| 30-OCT-23 | Office Supplies | 469899 |
| 31-OCT-23 | Software | 207020 |
| 31-OCT-23 | Hardware | 162079 |
| 31-OCT-23 | Office Supplies | 300016 |
| 01-NOV-23 | Software | 135323 |
| 01-NOV-23 | Hardware | 346351 |
| 01-NOV-23 | Office Supplies | 455117 |
| 02-NOV-23 | Software | 141936 |
| 02-NOV-23 | Hardware | 137447 |
| 02-NOV-23 | Office Supplies | 181300 |
| 03-NOV-23 | Software | 205830 |
| 03-NOV-23 | Hardware | 159631 |
| 03-NOV-23 | Office Supplies | 266341 |
| 04-NOV-23 | Software | 315384 |
| 04-NOV-23 | Hardware | 310670 |
| 04-NOV-23 | Office Supplies | 416700 |
| 05-NOV-23 | Software | 258691 |
| 05-NOV-23 | Hardware | 186587 |
| 05-NOV-23 | Office Supplies | 307950 |
| 06-NOV-23 | Software | 148380 |
| 06-NOV-23 | Hardware | 351308 |
| 06-NOV-23 | Office Supplies | 167538 |
| 07-NOV-23 | Software | 436947 |
| 07-NOV-23 | Hardware | 260935 |
| 07-NOV-23 | Office Supplies | 382351 |
| 08-NOV-23 | Software | 342330 |
| 08-NOV-23 | Hardware | 338863 |
| 08-NOV-23 | Office Supplies | 391822 |
| 09-NOV-23 | Software | 240852 |
| 09-NOV-23 | Hardware | 337127 |
| 09-NOV-23 | Office Supplies | 334466 |
| 10-NOV-23 | Software | 441530 |
| 10-NOV-23 | Hardware | 206628 |
| 10-NOV-23 | Office Supplies | 197825 |
| 11-NOV-23 | Software | 474690 |
| 11-NOV-23 | Hardware | 294645 |
| 11-NOV-23 | Office Supplies | 420051 |
| 12-NOV-23 | Software | 264250 |
| 12-NOV-23 | Hardware | 325605 |
| 12-NOV-23 | Office Supplies | 166001 |
| 13-NOV-23 | Software | 115899 |
| 13-NOV-23 | Hardware | 250878 |
| 13-NOV-23 | Office Supplies | 464325 |
| 14-NOV-23 | Software | 158215 |
| 14-NOV-23 | Hardware | 414510 |
| 14-NOV-23 | Office Supplies | 408215 |
| 15-NOV-23 | Software | 233715 |
| 15-NOV-23 | Hardware | 250923 |
| 15-NOV-23 | Office Supplies | 356489 |
| 16-NOV-23 | Software | 298662 |
| 16-NOV-23 | Hardware | 108081 |
| 16-NOV-23 | Office Supplies | 338672 |
| 17-NOV-23 | Software | 125303 |
| 17-NOV-23 | Hardware | 211584 |
| 17-NOV-23 | Office Supplies | 153773 |
| 18-NOV-23 | Software | 352250 |
| 18-NOV-23 | Hardware | 449350 |
| 18-NOV-23 | Office Supplies | 400943 |
| 19-NOV-23 | Software | 191838 |
| 19-NOV-23 | Hardware | 276175 |
| 19-NOV-23 | Office Supplies | 183478 |
| 20-NOV-23 | Software | 289584 |
| 20-NOV-23 | Hardware | 483466 |
| 20-NOV-23 | Office Supplies | 244075 |
| 21-NOV-23 | Software | 279126 |
| 21-NOV-23 | Hardware | 180520 |
| 21-NOV-23 | Office Supplies | 354276 |
| 22-NOV-23 | Software | 186392 |
| 22-NOV-23 | Hardware | 479634 |
| 22-NOV-23 | Office Supplies | 136275 |
| 23-NOV-23 | Software | 482701 |
| 23-NOV-23 | Hardware | 274590 |
| 23-NOV-23 | Office Supplies | 337465 |
| 24-NOV-23 | Software | 272275 |
| 24-NOV-23 | Hardware | 206596 |
| 24-NOV-23 | Office Supplies | 265819 |
| 25-NOV-23 | Software | 475753 |
| 25-NOV-23 | Hardware | 170663 |
| 25-NOV-23 | Office Supplies | 411858 |
| 26-NOV-23 | Software | 238840 |
| 26-NOV-23 | Hardware | 140992 |
| 26-NOV-23 | Office Supplies | 346945 |
| 27-NOV-23 | Software | 230495 |
| 27-NOV-23 | Hardware | 266155 |
| 27-NOV-23 | Office Supplies | 448711 |
| 28-NOV-23 | Software | 388240 |
| 28-NOV-23 | Hardware | 293781 |
| 28-NOV-23 | Office Supplies | 155978 |
| 29-NOV-23 | Software | 498768 |
| 29-NOV-23 | Hardware | 166529 |
| 29-NOV-23 | Office Supplies | 470821 |
| 30-NOV-23 | Software | 103530 |
| 30-NOV-23 | Hardware | 453834 |
| 30-NOV-23 | Office Supplies | 309072 |
| 01-DEC-23 | Software | 310076 |
| 01-DEC-23 | Hardware | 295026 |
| 01-DEC-23 | Office Supplies | 331398 |
| 02-DEC-23 | Software | 318602 |
| 02-DEC-23 | Hardware | 244608 |
| 02-DEC-23 | Office Supplies | 394145 |
| 03-DEC-23 | Software | 444490 |
| 03-DEC-23 | Hardware | 216417 |
| 03-DEC-23 | Office Supplies | 425513 |
| 04-DEC-23 | Software | 193954 |
| 04-DEC-23 | Hardware | 470937 |
| 04-DEC-23 | Office Supplies | 214677 |
| 05-DEC-23 | Software | 104492 |
| 05-DEC-23 | Hardware | 101056 |
| 05-DEC-23 | Office Supplies | 282247 |
| 06-DEC-23 | Software | 465632 |
| 06-DEC-23 | Hardware | 491091 |
| 06-DEC-23 | Office Supplies | 490342 |
| 07-DEC-23 | Software | 247888 |
| 07-DEC-23 | Hardware | 322334 |
| 07-DEC-23 | Office Supplies | 442330 |
| 08-DEC-23 | Software | 132190 |
| 08-DEC-23 | Hardware | 177824 |
| 08-DEC-23 | Office Supplies | 277247 |
| 09-DEC-23 | Software | 300054 |
| 09-DEC-23 | Hardware | 277895 |
| 09-DEC-23 | Office Supplies | 247049 |
| 10-DEC-23 | Software | 325098 |
| 10-DEC-23 | Hardware | 189922 |
| 10-DEC-23 | Office Supplies | 433469 |
| 11-DEC-23 | Software | 345347 |
| 11-DEC-23 | Hardware | 292778 |
| 11-DEC-23 | Office Supplies | 469616 |
| 12-DEC-23 | Software | 168864 |
| 12-DEC-23 | Hardware | 490877 |
| 12-DEC-23 | Office Supplies | 351204 |
| 13-DEC-23 | Software | 159965 |
| 13-DEC-23 | Hardware | 420244 |
| 13-DEC-23 | Office Supplies | 287081 |
| 14-DEC-23 | Software | 337371 |
| 14-DEC-23 | Hardware | 332795 |
| 14-DEC-23 | Office Supplies | 111930 |
| 15-DEC-23 | Software | 461622 |
| 15-DEC-23 | Hardware | 234988 |
| 15-DEC-23 | Office Supplies | 267212 |
| 16-DEC-23 | Software | 230958 |
| 16-DEC-23 | Hardware | 353872 |
| 16-DEC-23 | Office Supplies | 284872 |
| 17-DEC-23 | Software | 146321 |
| 17-DEC-23 | Hardware | 246657 |
| 17-DEC-23 | Office Supplies | 388863 |
| 18-DEC-23 | Software | 413152 |
| 18-DEC-23 | Hardware | 135720 |
| 18-DEC-23 | Office Supplies | 131658 |
| 19-DEC-23 | Software | 486320 |
| 19-DEC-23 | Hardware | 110131 |
| 19-DEC-23 | Office Supplies | 472921 |
| 20-DEC-23 | Software | 478448 |
| 20-DEC-23 | Hardware | 143700 |
| 20-DEC-23 | Office Supplies | 334531 |
| 21-DEC-23 | Software | 327614 |
| 21-DEC-23 | Hardware | 289838 |
| 21-DEC-23 | Office Supplies | 409196 |
| 22-DEC-23 | Software | 395130 |
| 22-DEC-23 | Hardware | 262819 |
| 22-DEC-23 | Office Supplies | 461814 |
| 23-DEC-23 | Software | 465882 |
| 23-DEC-23 | Hardware | 164457 |
| 23-DEC-23 | Office Supplies | 421300 |
| 24-DEC-23 | Software | 469328 |
| 24-DEC-23 | Hardware | 303003 |
| 24-DEC-23 | Office Supplies | 323886 |
| 25-DEC-23 | Software | 102272 |
| 25-DEC-23 | Hardware | 209510 |
| 25-DEC-23 | Office Supplies | 457322 |
| 26-DEC-23 | Software | 209543 |
| 26-DEC-23 | Hardware | 263148 |
| 26-DEC-23 | Office Supplies | 431696 |
| 27-DEC-23 | Software | 462120 |
| 27-DEC-23 | Hardware | 346375 |
| 27-DEC-23 | Office Supplies | 424553 |
| 28-DEC-23 | Software | 135913 |
| 28-DEC-23 | Hardware | 238250 |
| 28-DEC-23 | Office Supplies | 225642 |
| 29-DEC-23 | Software | 465128 |
| 29-DEC-23 | Hardware | 331667 |
| 29-DEC-23 | Office Supplies | 302910 |
| 30-DEC-23 | Software | 442538 |
| 30-DEC-23 | Hardware | 147313 |
| 30-DEC-23 | Office Supplies | 134577 |
| 31-DEC-23 | Software | 185736 |
| 31-DEC-23 | Hardware | 387580 |
| 31-DEC-23 | Office Supplies | 210082 |
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_month | total_sale |
|---|---|
| 01-JAN-23 | 30092600 |
| 01-FEB-23 | 25048255 |
| 01-MAR-23 | 28982026 |
| 01-APR-23 | 27177069 |
| 01-MAY-23 | 26798754 |
| 01-JUN-23 | 27854615 |
| 01-JUL-23 | 26549507 |
| 01-AUG-23 | 28133612 |
| 01-SEP-23 | 27473052 |
| 01-OCT-23 | 27481206 |
| 01-NOV-23 | 26136439 |
| 01-DEC-23 | 28666163 |
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_month | total_sale | previous_month_total_sale |
|---|---|---|
| 01-JAN-23 | 30092600 | – |
| 01-FEB-23 | 25048255 | 30092600 |
| 01-MAR-23 | 28982026 | 25048255 |
| 01-APR-23 | 27177069 | 28982026 |
| 01-MAY-23 | 26798754 | 27177069 |
| 01-JUN-23 | 27854615 | 26798754 |
| 01-JUL-23 | 26549507 | 27854615 |
| 01-AUG-23 | 28133612 | 26549507 |
| 01-SEP-23 | 27473052 | 28133612 |
| 01-OCT-23 | 27481206 | 27473052 |
| 01-NOV-23 | 26136439 | 27481206 |
| 01-DEC-23 | 28666163 | 26136439 |
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_month | total_sale | previous_month_total_sale | difference |
|---|---|---|---|
| 01-JAN-23 | 30092600 | – | – |
| 01-FEB-23 | 25048255 | 30092600 | -5044345 |
| 01-MAR-23 | 28982026 | 25048255 | 3933771 |
| 01-APR-23 | 27177069 | 28982026 | -1804957 |
| 01-MAY-23 | 26798754 | 27177069 | -378315 |
| 01-JUN-23 | 27854615 | 26798754 | 1055861 |
| 01-JUL-23 | 26549507 | 27854615 | -1305108 |
| 01-AUG-23 | 28133612 | 26549507 | 1584105 |
| 01-SEP-23 | 27473052 | 28133612 | -660560 |
| 01-OCT-23 | 27481206 | 27473052 | 8154 |
| 01-NOV-23 | 26136439 | 27481206 | -1344767 |
| 01-DEC-23 | 28666163 | 26136439 | 2529724 |
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_month | total_sale | previous_month_total_sale | difference | percent_change |
|---|---|---|---|---|
| 01-JAN-23 | 30092600 | – | – | – |
| 01-FEB-23 | 25048255 | 30092600 | -5044345 | -20.14 |
| 01-MAR-23 | 28982026 | 25048255 | 3933771 | 13.57 |
| 01-APR-23 | 27177069 | 28982026 | -1804957 | -6.64 |
| 01-MAY-23 | 26798754 | 27177069 | -378315 | -1.41 |
| 01-JUN-23 | 27854615 | 26798754 | 1055861 | 3.79 |
| 01-JUL-23 | 26549507 | 27854615 | -1305108 | -4.92 |
| 01-AUG-23 | 28133612 | 26549507 | 1584105 | 5.63 |
| 01-SEP-23 | 27473052 | 28133612 | -660560 | -2.4 |
| 01-OCT-23 | 27481206 | 27473052 | 8154 | .03 |
| 01-NOV-23 | 26136439 | 27481206 | -1344767 | -5.15 |
| 01-DEC-23 | 28666163 | 26136439 | 2529724 | 8.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_month | product_category | total_sale | previous_month_total_sale | difference | percent_change |
|---|---|---|---|---|---|
| 01-JAN-23 | Hardware | 10532049 | – | – | – |
| 01-JAN-23 | Office Supplies | 9527596 | – | – | – |
| 01-JAN-23 | Software | 10032955 | – | – | – |
| 01-FEB-23 | Hardware | 8145411 | 10532049 | -2386638 | -29.3 |
| 01-FEB-23 | Office Supplies | 8636016 | 9527596 | -891580 | -10.32 |
| 01-FEB-23 | Software | 8266828 | 10032955 | -1766127 | -21.36 |
| 01-MAR-23 | Hardware | 10107952 | 8145411 | 1962541 | 19.42 |
| 01-MAR-23 | Office Supplies | 9308036 | 8636016 | 672020 | 7.22 |
| 01-MAR-23 | Software | 9566038 | 8266828 | 1299210 | 13.58 |
| 01-APR-23 | Hardware | 9981157 | 10107952 | -126795 | -1.27 |
| 01-APR-23 | Office Supplies | 9101319 | 9308036 | -206717 | -2.27 |
| 01-APR-23 | Software | 8094593 | 9566038 | -1471445 | -18.18 |
| 01-MAY-23 | Hardware | 8224588 | 9981157 | -1756569 | -21.36 |
| 01-MAY-23 | Office Supplies | 9854939 | 9101319 | 753620 | 7.65 |
| 01-MAY-23 | Software | 8719227 | 8094593 | 624634 | 7.16 |
| 01-JUN-23 | Hardware | 8983925 | 8224588 | 759337 | 8.45 |
| 01-JUN-23 | Office Supplies | 9033310 | 9854939 | -821629 | -9.1 |
| 01-JUN-23 | Software | 9837380 | 8719227 | 1118153 | 11.37 |
| 01-JUL-23 | Hardware | 8956408 | 8983925 | -27517 | -.31 |
| 01-JUL-23 | Office Supplies | 8742601 | 9033310 | -290709 | -3.33 |
| 01-JUL-23 | Software | 8850498 | 9837380 | -986882 | -11.15 |
| 01-AUG-23 | Hardware | 9718295 | 8956408 | 761887 | 7.84 |
| 01-AUG-23 | Office Supplies | 8566646 | 8742601 | -175955 | -2.05 |
| 01-AUG-23 | Software | 9848671 | 8850498 | 998173 | 10.14 |
| 01-SEP-23 | Hardware | 9198295 | 9718295 | -520000 | -5.65 |
| 01-SEP-23 | Office Supplies | 8742664 | 8566646 | 176018 | 2.01 |
| 01-SEP-23 | Software | 9532093 | 9848671 | -316578 | -3.32 |
| 01-OCT-23 | Hardware | 9408886 | 9198295 | 210591 | 2.24 |
| 01-OCT-23 | Office Supplies | 9149671 | 8742664 | 407007 | 4.45 |
| 01-OCT-23 | Software | 8922649 | 9532093 | -609444 | -6.83 |
| 01-NOV-23 | Hardware | 8334058 | 9408886 | -1074828 | -12.9 |
| 01-NOV-23 | Office Supplies | 9474652 | 9149671 | 324981 | 3.43 |
| 01-NOV-23 | Software | 8327729 | 8922649 | -594920 | -7.14 |
| 01-DEC-23 | Hardware | 8492832 | 8334058 | 158774 | 1.87 |
| 01-DEC-23 | Office Supplies | 10441283 | 9474652 | 966631 | 9.26 |
| 01-DEC-23 | Software | 9732048 | 8327729 | 1404319 | 14.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_quarter | total_sale | next_quarter_total_sale |
|---|---|---|
| 1 | 84122881 | 81830438 |
| 2 | 81830438 | 82156171 |
| 3 | 82156171 | 82283808 |
| 4 | 82283808 | – |
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_quarter | total_sale | first_quarter_total_sale | next_quarter_total_sale | difference | percent_change |
|---|---|---|---|---|---|
| 1 | 84122881 | 84122881 | 81830438 | 2292443 | -2.8 |
| 2 | 81830438 | 84122881 | 82156171 | 1966710 | -2.39 |
| 3 | 82156171 | 84122881 | 82283808 | 1839073 | -2.24 |
| 4 | 82283808 | 84122881 | – | – | – |
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.