Accounting Analytics: An Integrated Approach
Part III of V: Accounting Analytics with SQL
Chapter 1 of 4 in this Part
SELECT * returns every column; a list returns only those, in that orderTable 9.1 · Common SQLite error messages and their usual causes
| Message | Usual cause |
|---|---|
no such column: AccountNumbr |
A column name misspelled, or a text value written without quotes |
no such table: Accounts |
A table name misspelled |
near "WHERE": syntax error |
Clauses out of order, or a comma or keyword missing before the word named |
unrecognized token: "'Asset" |
A quotation mark opened but never closed |
+, -, * and / to each rowTable 9.2 · Scalar functions and expressions used in this Part
| Function | Returns | Example | Result |
|---|---|---|---|
ROUND(x, d) |
x rounded to d decimal places | ROUND(1234.567, 2) |
1234.57 |
ABS(x) |
x without its sign | ABS(-2011.14) |
2011.14 |
CAST(x AS type) |
x converted to INTEGER, REAL, or TEXT | CAST('2026' AS INTEGER) |
2026 |
UPPER(x) |
x in capital letters | UPPER('llc') |
LLC |
TRIM(x) |
x without spaces at either end | TRIM(' LLC ') |
LLC |
Table 9.2 · Scalar functions and expressions used in this Part
| Function | Returns | Example | Result |
|---|---|---|---|
REPLACE(x, a, b) |
x with every a replaced by b | REPLACE('WO-2026', '-', '') |
WO2026 |
LENGTH(x) |
the number of characters in x | LENGTH('WO-2026') |
7 |
SUBSTR(x, s, n) |
n characters of x, from position s | SUBSTR('WO-2026', 4, 4) |
2026 |
SUBSTR(x, s) |
the rest of x, from position s | SUBSTR('JE-2026-000296', 9) |
000296 |
COALESCE(x, y) |
x, or y when x is NULL | COALESCE(NULL, 'open') |
open |
SELECT DISTINCT SourceDocumentType FROM GLEntry lists each kind of source onceWhat does each expression return in SQLite?
7 / 27 / 2.0ROUND(1234.567, -2)UPPER(TRIM(' llc '))Check that:
UPPER(TRIM(x)) compares text whose case or spaces cannot be trustedTable 9.3 · Comparison and logical operators in a WHERE clause
| Operator | Meaning | Example |
|---|---|---|
= and <> |
Equal to, not equal to | AccountID = 93 |
<, >, <=, >= |
Less than, greater than, or equal | Debit > 1000 |
BETWEEN a AND b |
Within a range, including both ends | FiscalYear BETWEEN 2025 AND 2026 |
IN (a, b, c) |
Equal to any value in a list | AccountNumber IN (1090, 5080) |
LIKE |
Matches a pattern | VoucherNumber LIKE 'JE-%' |
IS NULL, IS NOT NULL |
Has no value, has a value | ClosedDate IS NULL |
AND, OR, NOT |
Combine or reverse conditions | NOT Status = 'Closed' |
% for any run of characters, _ for oneHow many work orders or ledger rows does each condition keep?
ClosedDate = NULLClosedDate IS NULLSourceDocumentType = 'workorderclose'SourceDocumentType LIKE 'workorderclose'Check that:
ClosedDate IS NULL finds the open work orders, and ClosedDate = NULL finds none;SUBSTR(WorkOrderNumber, 4, 4) = 2026 finds no rows, while = '2026' finds every open work order.-- starts a one-line comment; /* and */ enclose several linesSELECT AccountNumber AccountName runs: the second name becomes an aliasName the logical error in each case.
Check that:
Calculated column · Clause · Column alias · Comment · Declarative language · DISTINCT · Logical error · Manufacturing variance · NULL · Operator precedence · Query · Result set · Scalar function · SQL (Structured Query Language) · SQL dialect · SQL script · SQLite · Standard cost · Syntax error · WHERE clause · Wildcard · Working copy
Next: Chapter 10: Joining and Summarizing Data 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/09-sql-essentials/chapter.html
Chapter 9 · SQL Essentials for Accounting Analytics