Accounting Analytics: An Integrated Approach
Part III of V: Accounting Analytics with SQL
Chapter 2 of 4 in this Part
SELECT woc.WorkOrderCloseID, i.ItemGroup,
woc.TotalVarianceAmount
FROM WorkOrderClose AS woc
INNER JOIN WorkOrder AS wo
ON wo.WorkOrderID = woc.WorkOrderID
INNER JOIN Item AS i ON i.ItemID = wo.ItemID;woc keep a query short; qualify every column in a joinTable 10.1 · Join paths through the purchasing, manufacturing, payroll, and ledger tables
| From | To | Join on |
|---|---|---|
| PurchaseOrder | Supplier | SupplierID |
| PurchaseOrder | Employee, as creator | CreatedByEmployeeID = EmployeeID |
| PurchaseOrder | Employee, as approver | ApprovedByEmployeeID = EmployeeID |
| PurchaseOrderLine | PurchaseOrder | PurchaseOrderID |
| GoodsReceiptLine | PurchaseOrderLine | POLineID |
| PurchaseInvoiceLine | PurchaseInvoice | PurchaseInvoiceID |
| PurchaseInvoiceLine | PurchaseOrderLine | POLineID |
| PurchaseInvoiceLine | GoodsReceiptLine | GoodsReceiptLineID |
| PurchaseInvoice | Supplier | SupplierID |
| DisbursementPayment | PurchaseInvoice | PurchaseInvoiceID |
| DisbursementPayment | Supplier | SupplierID |
Table 10.1 · Join paths through the purchasing, manufacturing, payroll, and ledger tables
| From | To | Join on |
|---|---|---|
| WorkOrder | Item | ItemID |
| WorkOrderOperation | WorkOrder | WorkOrderID |
| WorkOrderOperation | WorkCenter | WorkCenterID |
| WorkOrderClose | WorkOrder | WorkOrderID |
| ProductionCompletionLine | ProductionCompletion | ProductionCompletionID |
| ProductionCompletionLine | Item | ItemID |
| LaborTimeEntry | WorkOrderOperation | WorkOrderOperationID |
Table 10.1 · Join paths through the purchasing, manufacturing, payroll, and ledger tables
| From | To | Join on |
|---|---|---|
| TimeClockEntry, LaborTimeEntry | Employee | EmployeeID |
| PayrollRegisterLine | PayrollRegister | PayrollRegisterID |
| PayrollRegister | PayrollPeriod | PayrollPeriodID |
| PayrollRegister | Employee, CostCenter | EmployeeID, CostCenterID |
| PayrollPayment | PayrollRegister | PayrollRegisterID |
| GLEntry | Account, CostCenter | AccountID, CostCenterID |
| GLEntry | A source document table | SourceDocumentType and SourceDocumentID |
gl.SourceDocumentType = 'JournalEntry'Which join answers each question?
Table 10.2 · Aggregate functions and how they treat missing values
| Function | Returns | Treatment of NULL |
|---|---|---|
COUNT(*) |
The number of rows | Counts every row |
COUNT(column) |
The number of values in a column | Skips NULLs |
COUNT(DISTINCT column) |
The number of different values | Skips NULLs |
SUM(column) |
The total | Skips NULLs |
AVG(column) |
The average | Skips NULLs, so they do not count as zero |
MIN(column), MAX(column) |
The smallest and largest values | Skip NULLs |
GROUP_CONCAT(column, sep) |
The values joined into one text, separated by sep, or by commas if sep is left out | Skips NULLs |
WHERE or HAVING?
Table 10.3 · Data quality tests in SQL
| Dimension | SQL techniques |
|---|---|
| Accuracy | MIN and MAX of amounts; a WHERE condition that compares two dates; GROUP BY the key with HAVING COUNT(*) > 1 |
| Completeness | COUNT(column) against COUNT(*); COUNT(*) by month; GROUP BY a type column |
| Consistency | GROUP BY two related columns; an anti-join to the parent table; the distinct values of a code column |
| Timeliness | MIN and MAX of the dates; COUNT(*) by month at each end |
Check that:
What is wrong with each result?
Check that:
Check that:
COUNT(*) counts registers and COUNT(DISTINCT pr.EmployeeID) counts people;Aggregate function · Anti-join · Correlated subquery · Data profile · Derived table · Fan-out · GROUP BY clause · HAVING · Inner join · Join · Left outer join · Logical evaluation order · NOT EXISTS · Production-volume variance · Scalar subquery · Standard hours allowed · Subquery · Table alias · Trial balance
Next: Chapter 11: Analytical Queries 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/10-joining-and-summarizing/chapter.html
Chapter 10 · Joining and Summarizing Data in SQL