Accounting Analytics: An Integrated Approach
Part III of V: Accounting Analytics with SQL
Chapter 3 of 4 in this Part
WHEN NULL never matchesORDER BY CASE sorts in an order no column holds, such as statement orderTable 11.1 · SQLite date functions, for the date d of 2026-03-06
| Function | Returns | Example result |
|---|---|---|
strftime('%Y', d) |
The year, as text | 2026 |
strftime('%m', d) |
The month, as two digits | 03 |
strftime('%Y-%m', d) |
The year and month | 2026-03 |
strftime('%w', d) |
The weekday, 0 for Sunday to 6 for Saturday | 5 |
date(d) |
The date without its time | 2026-03-06 |
date(d, 'start of month') |
The first day of the month | 2026-03-01 |
date(d, '+1 month', '-1 day') |
A date moved by the modifiers, in order | 2026-04-05 |
julianday(d) |
The date as a number of days | 2461105.5 |
date(d, 'start of month', '+1 month', '-1 day') is the month’s last dayCOUNT(CASE WHEN … THEN 1 END) counts rows, because COUNT skips the NULLsSUM(CASE WHEN … THEN 1 ELSE 0 END) gives the same count as a 1-or-0 flagWhich tool does each task?
strftime('%Y-%m', d) in GROUP BYdate with ‘start of month’, ‘+1 month’ and ‘-1 day’, in that orderCheck that:
OVER () looks at every row; PARTITION BY at the rows that share its columnsROWS UNBOUNDED PRECEDINGROWS BETWEEN 11 PRECEDING AND CURRENT ROW is a trailing twelve monthsWINDOW w AS (...) names a window that several functions shareLAG(x) partitioned by month: the latest earlier year with that monthTable 11.2 · Window functions used in this chapter
| Function | Returns |
|---|---|
ROW_NUMBER() |
A sequence number within the partition: 1, 2, 3, even for ties |
RANK(), DENSE_RANK() |
A rank in which ties share a number; RANK skips numbers after a tie |
SUM(x) OVER (...) |
A total over the window: the whole partition, or a running or moving total |
AVG, COUNT, MIN, MAX |
The same aggregates over the window |
LAG(x, n), LEAD(x, n) |
The value of x n rows before, or after, the current row |
Table 11.3 · Clauses that define a window
| Clause | Effect |
|---|---|
OVER () |
The window is every row of the result |
PARTITION BY |
Divides the rows into groups that the window never crosses |
ORDER BY |
Orders each partition; for an aggregate, the default window runs to the current row and its peers |
ROWS BETWEEN n PRECEDING AND CURRENT ROW |
The current row and the n rows before it |
ROWS UNBOUNDED PRECEDING |
Every row from the first of the partition to the current one |
WINDOW w AS (...) |
Names a window, for use as OVER w |
Which window does each question need?
ROWS BETWEEN 11 PRECEDING AND CURRENT ROWCheck that:
WITH name AS (…) defines a named subquery at the start of a queryDROP VIEW IF EXISTS MonthlyLaborEfficiency;
CREATE VIEW MonthlyLaborEfficiency AS
WITH ...
SELECT ...;CREATE TEMP VIEW lasts one session and works even read-onlyWhat happens in each case?
Check that:
CASE expression · Common table expression (CTE) · Conditional aggregation · Date function · Frame · LAG and LEAD · Named window · Partition · Ranking function · Running total · Trailing twelve months · View · Window function
Next: Chapter 12: Audit Analytics in SQL
These slides accompany Accounting Analytics: An Integrated Approach, licensed under CC BY-SA 4.0. Every figure and table comes from the chapter:
https://aa.accountinganalyticshub.com/chapters/11-analytical-queries/chapter.html
Chapter 11 · Analytical Queries in SQL