Case 1: budget vs actual in SQL
The cost center variance report is the first thing finance gets asked for every month. You build it for a single month and for the half-year, with variance as a percentage and a flag that only marks the lines worth discussing.
In short: actuals are SUM(l.debit - l.credit) over a cost center's ledger lines in the period you want. The budget comes from the budget table, one row per cost center and one column per month. Variance is actual minus budget, and the percentage is always taken on budget. For the flag, CASE tests two thresholds at once.
The situation
It's early July at a maker of bike accessories, and the finance director is preparing the half-year review. She wants two things: the March cost report, because that's when the spring campaign ran, and a half-year comparison per cost center that only flags the variances big enough to talk about. Until now, someone exported the ledger and matched it against the budget with SUMIFS. This time the query should run straight on the database.
The tables
- ledger: journal lines from the accounting system. Each line has an entry_date, an account_id, an amount as either debit or credit and, if it's a cost, a cost_center_id.
- cost_centers: eight cost centers with name and department.
- budget: the 2026 cost budget. One row per cost center with the months in the columns jan to dec, the way a budget usually arrives from a planning workbook.
One thing matters for actuals: costs are debits and credit notes are credits. Sum only the debits and you miss every credit note. Here are March's lines for Manufacturing (cost center 2000):
SELECT doc_no, entry_date, account_id, description, debit, credit
FROM ledger
WHERE cost_center_id = 2000
AND entry_date BETWEEN '2026-03-01' AND '2026-03-31';
| doc_no | entry_date | account_id | description | debit | credit |
|---|---|---|---|---|---|
| 1018 | 2026-03-03 | 6100 | Rent March | 6800 | 0 |
| 1020 | 2026-03-08 | 5000 | Materials purchase | 110523.12 | 0 |
| 1023 | 2026-03-20 | 6200 | Electricity and gas | 3929.91 | 0 |
| 1024 | 2026-03-22 | 6600 | Machine service | 2480 | 0 |
| 1025 | 2026-03-26 | 5000 | Supplier credit note return | 0 | 1240 |
| 1026 | 2026-03-28 | 6000 | Payroll | 24522.86 | 0 |
Goods sent back to the supplier cut materials by 1,240. With SUM(debit - credit) that's taken care of automatically.
Task 1: the March report
Task The finance director wants the budget vs actual report for March 2026. For each cost center show: cost_center (name), budget (the March budget, column mar in budget), actual (March costs from the ledger, that is debit minus credit) and variance (actual minus budget).
cost_centers 8 rows, 4 columns
| cost_center_id | name | department | manager |
|---|---|---|---|
| 1000 | Management | Administration | Laura Bennett |
| 1100 | Finance and HR | Administration | Daniel Price |
| 1200 | IT | Administration | Ryan Cooper |
| 2000 | Manufacturing | Operations | Mark Ellis |
| 2100 | Assembly | Operations | Sofia Ramirez |
| 2200 | Warehouse and shipping | Operations | Kevin Shaw |
| 3000 | Sales | Sales | James Turner |
| 3100 | Marketing | Sales | Emily Ward |
budget 8 rows, 13 columns
| cost_center_id | jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1000 | 19300 | 18800 | 19700 | 19300 | 18700 | 18800 | 19600 | 19700 | 18700 | 19000 | 19300 | 19000 |
| 1100 | 11500 | 11500 | 11500 | 11200 | 11300 | 11400 | 11500 | 11300 | 11000 | 11300 | 11000 | 11300 |
| 1200 | 12700 | 8100 | 9300 | 12700 | 8100 | 10200 | 10100 | 10100 | 10300 | 10100 | 10600 | 10100 |
| 2000 | 92900 | 107800 | 146000 | 173100 | 159400 | 140400 | 135500 | 135100 | 141600 | 141100 | 139100 | 138400 |
| 2100 | 56400 | 63800 | 91200 | 99400 | 94600 | 87200 | 83700 | 86400 | 86300 | 82200 | 84500 | 82900 |
| 2200 | 9900 | 10200 | 9900 | 10300 | 10100 | 10100 | 9600 | 10200 | 10100 | 9700 | 9900 | 9700 |
| 3000 | 13100 | 14500 | 14600 | 15800 | 13100 | 13900 | 14900 | 14800 | 15000 | 14700 | 14300 | 14300 |
| 3100 | 8900 | 10900 | 18900 | 18900 | 12900 | 12900 | 15400 | 15500 | 15600 | 15900 | 16300 | 15500 |
ledger 169 rows, 8 columns
| entry_id | doc_no | entry_date | account_id | cost_center_id | description | debit | credit |
|---|---|---|---|---|---|---|---|
| 1 | 1001 | 2026-01-01 | 1000 | NULL | Opening balance bank | 320000 | 0 |
| 2 | 1001 | 2026-01-01 | 3000 | NULL | Opening balance bank | 0 | 320000 |
| 3 | 1002 | 2026-01-03 | 6100 | 2000 | Rent January | 6800 | 0 |
| 4 | 1002 | 2026-01-03 | 6100 | 1000 | Rent January | 4600 | 0 |
| 5 | 1002 | 2026-01-03 | 1000 | NULL | Rent January | 0 | 11400 |
| 6 | 1003 | 2026-01-05 | 6500 | 1200 | Software licences quarter | 4860 | 0 |
| 7 | 1003 | 2026-01-05 | 2000 | NULL | Software licences quarter | 0 | 4860 |
| 8 | 1004 | 2026-01-08 | 5000 | 2000 | Materials purchase | 56177.71 | 0 |
| 9 | 1004 | 2026-01-08 | 5000 | 2100 | Materials purchase | 34355.83 | 0 |
| 10 | 1004 | 2026-01-08 | 2000 | NULL | Materials purchase | 0 | 90533.54 |
| 11 | 1005 | 2026-01-20 | 6200 | 2000 | Electricity and gas | 3416.46 | 0 |
| 12 | 1005 | 2026-01-20 | 2000 | NULL | Electricity and gas | 0 | 3416.46 |
| 13 | 1006 | 2026-01-28 | 6000 | 1000 | Payroll | 14469.07 | 0 |
| 14 | 1006 | 2026-01-28 | 6000 | 1100 | Payroll | 11248.67 | 0 |
| 15 | 1006 | 2026-01-28 | 6000 | 1200 | Payroll | 7764.75 | 0 |
| 16 | 1006 | 2026-01-28 | 6000 | 2000 | Payroll | 24648.2 | 0 |
| 17 | 1006 | 2026-01-28 | 6000 | 2100 | Payroll | 19865.04 | 0 |
| 18 | 1006 | 2026-01-28 | 6000 | 2200 | Payroll | 9919.49 | 0 |
| 19 | 1006 | 2026-01-28 | 6000 | 3000 | Payroll | 13339.13 | 0 |
| 20 | 1006 | 2026-01-28 | 6000 | 3100 | Payroll | 6905.66 | 0 |
| 21 | 1006 | 2026-01-28 | 1000 | NULL | Payroll | 0 | 108160.01 |
| 22 | 1007 | 2026-01-31 | 1100 | NULL | Sales invoices | 190302.72 | 0 |
| 23 | 1007 | 2026-01-31 | 4000 | NULL | Sales invoices | 0 | 190302.72 |
| 24 | 1008 | 2026-01-31 | 1000 | NULL | Customer receipts | 41681.77 | 0 |
| 25 | 1008 | 2026-01-31 | 1100 | NULL | Customer receipts | 0 | 41681.77 |
| 26 | 1009 | 2026-02-03 | 6100 | 2000 | Rent February | 6800 | 0 |
| 27 | 1009 | 2026-02-03 | 6100 | 1000 | Rent February | 4600 | 0 |
| 28 | 1009 | 2026-02-03 | 1000 | NULL | Rent February | 0 | 11400 |
| 29 | 1010 | 2026-02-08 | 5000 | 2000 | Materials purchase | 73668.51 | 0 |
| 30 | 1010 | 2026-02-08 | 5000 | 2100 | Materials purchase | 44273.2 | 0 |
| 31 | 1010 | 2026-02-08 | 2000 | NULL | Materials purchase | 0 | 117941.71 |
| 32 | 1011 | 2026-02-10 | 2000 | NULL | Supplier payments | 98810 | 0 |
| 33 | 1011 | 2026-02-10 | 1000 | NULL | Supplier payments | 0 | 98810 |
| 34 | 1012 | 2026-02-12 | 6400 | 3100 | Ads | 4200 | 0 |
| 35 | 1012 | 2026-02-12 | 2000 | NULL | Ads | 0 | 4200 |
| 36 | 1013 | 2026-02-20 | 6200 | 2000 | Electricity and gas | 3671.31 | 0 |
| 37 | 1013 | 2026-02-20 | 2000 | NULL | Electricity and gas | 0 | 3671.31 |
| 38 | 1014 | 2026-02-25 | 6300 | 3000 | Sales travel expenses | 1286.4 | 0 |
| 39 | 1014 | 2026-02-25 | 1000 | NULL | Sales travel expenses | 0 | 1286.4 |
| 40 | 1015 | 2026-02-28 | 6000 | 1000 | Payroll | 14564.78 | 0 |
| 41 | 1015 | 2026-02-28 | 6000 | 1100 | Payroll | 11248.62 | 0 |
| 42 | 1015 | 2026-02-28 | 6000 | 1200 | Payroll | 7830.19 | 0 |
| 43 | 1015 | 2026-02-28 | 6000 | 2000 | Payroll | 24669.51 | 0 |
| 44 | 1015 | 2026-02-28 | 6000 | 2100 | Payroll | 19869.44 | 0 |
| 45 | 1015 | 2026-02-28 | 6000 | 2200 | Payroll | 9893.72 | 0 |
| 46 | 1015 | 2026-02-28 | 6000 | 3000 | Payroll | 13366.23 | 0 |
| 47 | 1015 | 2026-02-28 | 6000 | 3100 | Payroll | 6921.81 | 0 |
| 48 | 1015 | 2026-02-28 | 1000 | NULL | Payroll | 0 | 108364.3 |
| 49 | 1016 | 2026-02-28 | 1100 | NULL | Sales invoices | 249339.75 | 0 |
| 50 | 1016 | 2026-02-28 | 4000 | NULL | Sales invoices | 0 | 249339.75 |
| 51 | 1017 | 2026-02-28 | 1000 | NULL | Customer receipts | 77712.92 | 0 |
| 52 | 1017 | 2026-02-28 | 1100 | NULL | Customer receipts | 0 | 77712.92 |
| 53 | 1018 | 2026-03-03 | 6100 | 2000 | Rent March | 6800 | 0 |
| 54 | 1018 | 2026-03-03 | 6100 | 1000 | Rent March | 4600 | 0 |
| 55 | 1018 | 2026-03-03 | 1000 | NULL | Rent March | 0 | 11400 |
| 56 | 1019 | 2026-03-05 | 6500 | 1200 | Laptop hardware | 3290 | 0 |
| 57 | 1019 | 2026-03-05 | 2000 | NULL | Laptop hardware | 0 | 3290 |
| 58 | 1020 | 2026-03-08 | 5000 | 2000 | Materials purchase | 110523.12 | 0 |
| 59 | 1020 | 2026-03-08 | 5000 | 2100 | Materials purchase | 67928.04 | 0 |
| 60 | 1020 | 2026-03-08 | 2000 | NULL | Materials purchase | 0 | 178451.16 |
| 61 | 1021 | 2026-03-10 | 2000 | NULL | Supplier payments | 125813.02 | 0 |
| 62 | 1021 | 2026-03-10 | 1000 | NULL | Supplier payments | 0 | 125813.02 |
| 63 | 1022 | 2026-03-12 | 6400 | 3100 | Spring campaign | 18600 | 0 |
| 64 | 1022 | 2026-03-12 | 2000 | NULL | Spring campaign | 0 | 18600 |
| 65 | 1023 | 2026-03-20 | 6200 | 2000 | Electricity and gas | 3929.91 | 0 |
| 66 | 1023 | 2026-03-20 | 2000 | NULL | Electricity and gas | 0 | 3929.91 |
| 67 | 1024 | 2026-03-22 | 6600 | 2000 | Machine service | 2480 | 0 |
| 68 | 1024 | 2026-03-22 | 2000 | NULL | Machine service | 0 | 2480 |
| 69 | 1025 | 2026-03-26 | 2000 | NULL | Supplier credit note return | 1240 | 0 |
| 70 | 1025 | 2026-03-26 | 5000 | 2000 | Supplier credit note return | 0 | 1240 |
| 71 | 1026 | 2026-03-28 | 6000 | 1000 | Payroll | 14521.48 | 0 |
| 72 | 1026 | 2026-03-28 | 6000 | 1100 | Payroll | 11254.45 | 0 |
| 73 | 1026 | 2026-03-28 | 6000 | 1200 | Payroll | 7770.97 | 0 |
| 74 | 1026 | 2026-03-28 | 6000 | 2000 | Payroll | 24522.86 | 0 |
| 75 | 1026 | 2026-03-28 | 6000 | 2100 | Payroll | 19761.5 | 0 |
| 76 | 1026 | 2026-03-28 | 6000 | 2200 | Payroll | 9898.42 | 0 |
| 77 | 1026 | 2026-03-28 | 6000 | 3000 | Payroll | 16593.37 | 0 |
| 78 | 1026 | 2026-03-28 | 6000 | 3100 | Payroll | 6919.28 | 0 |
| 79 | 1026 | 2026-03-28 | 1000 | NULL | Payroll | 0 | 111242.33 |
| 80 | 1027 | 2026-03-31 | 1100 | NULL | Sales invoices | 375915.93 | 0 |
| 81 | 1027 | 2026-03-31 | 4000 | NULL | Sales invoices | 0 | 375915.93 |
| 82 | 1028 | 2026-03-31 | 1000 | NULL | Customer receipts | 297540.71 | 0 |
| 83 | 1028 | 2026-03-31 | 1100 | NULL | Customer receipts | 0 | 297540.71 |
| 84 | 1029 | 2026-04-03 | 6100 | 2000 | Rent April | 6800 | 0 |
| 85 | 1029 | 2026-04-03 | 6100 | 1000 | Rent April | 4600 | 0 |
| 86 | 1029 | 2026-04-03 | 1000 | NULL | Rent April | 0 | 11400 |
| 87 | 1030 | 2026-04-05 | 6500 | 1200 | Software licences quarter | 4860 | 0 |
| 88 | 1030 | 2026-04-05 | 2000 | NULL | Software licences quarter | 0 | 4860 |
| 89 | 1031 | 2026-04-08 | 5000 | 2000 | Materials purchase | 132153.91 | 0 |
| 90 | 1031 | 2026-04-08 | 5000 | 2100 | Materials purchase | 84705.12 | 0 |
| 91 | 1031 | 2026-04-08 | 2000 | NULL | Materials purchase | 0 | 216859.03 |
| 92 | 1032 | 2026-04-10 | 2000 | NULL | Supplier payments | 205511.07 | 0 |
| 93 | 1032 | 2026-04-10 | 1000 | NULL | Supplier payments | 0 | 205511.07 |
| 94 | 1033 | 2026-04-12 | 6400 | 3100 | Spring campaign | 21400 | 0 |
| 95 | 1033 | 2026-04-12 | 2000 | NULL | Spring campaign | 0 | 21400 |
| 96 | 1034 | 2026-04-20 | 6200 | 2000 | Electricity and gas | 4275.47 | 0 |
| 97 | 1034 | 2026-04-20 | 2000 | NULL | Electricity and gas | 0 | 4275.47 |
| 98 | 1035 | 2026-04-25 | 6300 | 3000 | Sales travel expenses | 2914.7 | 0 |
| 99 | 1035 | 2026-04-25 | 1000 | NULL | Sales travel expenses | 0 | 2914.7 |
| 100 | 1036 | 2026-04-28 | 6000 | 1000 | Payroll | 14511.12 | 0 |
| 101 | 1036 | 2026-04-28 | 6000 | 1100 | Payroll | 11250.53 | 0 |
| 102 | 1036 | 2026-04-28 | 6000 | 1200 | Payroll | 7799.38 | 0 |
| 103 | 1036 | 2026-04-28 | 6000 | 2000 | Payroll | 24704.1 | 0 |
| 104 | 1036 | 2026-04-28 | 6000 | 2100 | Payroll | 22185.09 | 0 |
| 105 | 1036 | 2026-04-28 | 6000 | 2200 | Payroll | 9945.03 | 0 |
| 106 | 1036 | 2026-04-28 | 6000 | 3000 | Payroll | 13372.53 | 0 |
| 107 | 1036 | 2026-04-28 | 6000 | 3100 | Payroll | 6882.32 | 0 |
| 108 | 1036 | 2026-04-28 | 1000 | NULL | Payroll | 0 | 110650.1 |
| 109 | 1037 | 2026-04-30 | 1100 | NULL | Sales invoices | 452137.92 | 0 |
| 110 | 1037 | 2026-04-30 | 4000 | NULL | Sales invoices | 0 | 452137.92 |
| 111 | 1038 | 2026-04-30 | 1000 | NULL | Customer receipts | 235601.59 | 0 |
| 112 | 1038 | 2026-04-30 | 1100 | NULL | Customer receipts | 0 | 235601.59 |
| 113 | 1039 | 2026-05-03 | 6100 | 2000 | Rent May | 6800 | 0 |
| 114 | 1039 | 2026-05-03 | 6100 | 1000 | Rent May | 4600 | 0 |
| 115 | 1039 | 2026-05-03 | 1000 | NULL | Rent May | 0 | 11400 |
| 116 | 1040 | 2026-05-08 | 5000 | 2000 | Materials purchase | 141737.72 | 0 |
| 117 | 1040 | 2026-05-08 | 5000 | 2100 | Materials purchase | 82231.25 | 0 |
| 118 | 1040 | 2026-05-08 | 2000 | NULL | Materials purchase | 0 | 223968.97 |
| 119 | 1041 | 2026-05-10 | 2000 | NULL | Supplier payments | 247394.5 | 0 |
| 120 | 1041 | 2026-05-10 | 1000 | NULL | Supplier payments | 0 | 247394.5 |
| 121 | 1042 | 2026-05-12 | 6400 | 3100 | Ads | 6300 | 0 |
| 122 | 1042 | 2026-05-12 | 2000 | NULL | Ads | 0 | 6300 |
| 123 | 1043 | 2026-05-20 | 6200 | 2000 | Electricity and gas | 4015.69 | 0 |
| 124 | 1043 | 2026-05-20 | 2000 | NULL | Electricity and gas | 0 | 4015.69 |
| 125 | 1044 | 2026-05-27 | 2000 | NULL | Agency credit note | 1850 | 0 |
| 126 | 1044 | 2026-05-27 | 6400 | 3100 | Agency credit note | 0 | 1850 |
| 127 | 1045 | 2026-05-28 | 6000 | 1000 | Payroll | 14460.25 | 0 |
| 128 | 1045 | 2026-05-28 | 6000 | 1100 | Payroll | 11220.17 | 0 |
| 129 | 1045 | 2026-05-28 | 6000 | 1200 | Payroll | 7803.77 | 0 |
| 130 | 1045 | 2026-05-28 | 6000 | 2000 | Payroll | 24549.87 | 0 |
| 131 | 1045 | 2026-05-28 | 6000 | 2100 | Payroll | 22845.09 | 0 |
| 132 | 1045 | 2026-05-28 | 6000 | 2200 | Payroll | 9873.46 | 0 |
| 133 | 1045 | 2026-05-28 | 6000 | 3000 | Payroll | 13377.78 | 0 |
| 134 | 1045 | 2026-05-28 | 6000 | 3100 | Payroll | 6876.77 | 0 |
| 135 | 1045 | 2026-05-28 | 1000 | NULL | Payroll | 0 | 111007.16 |
| 136 | 1046 | 2026-05-31 | 1100 | NULL | Sales invoices | 437876.58 | 0 |
| 137 | 1046 | 2026-05-31 | 4000 | NULL | Sales invoices | 0 | 437876.58 |
| 138 | 1047 | 2026-05-31 | 1000 | NULL | Customer receipts | 396656.49 | 0 |
| 139 | 1047 | 2026-05-31 | 1100 | NULL | Customer receipts | 0 | 396656.49 |
| 140 | 1048 | 2026-06-03 | 6100 | 2000 | Rent June | 6800 | 0 |
| 141 | 1048 | 2026-06-03 | 6100 | 1000 | Rent June | 4600 | 0 |
| 142 | 1048 | 2026-06-03 | 1000 | NULL | Rent June | 0 | 11400 |
| 143 | 1049 | 2026-06-05 | 6500 | 1200 | Laptop hardware | 2140 | 0 |
| 144 | 1050 | 2026-06-08 | 5000 | 2000 | Materials purchase | 100972.77 | 0 |
| 145 | 1050 | 2026-06-08 | 5000 | 2100 | Materials purchase | 65827.08 | 0 |
| 146 | 1050 | 2026-06-08 | 2000 | NULL | Materials purchase | 0 | 166529.85 |
| 147 | 1051 | 2026-06-10 | 2000 | NULL | Supplier payments | 232434.66 | 0 |
| 148 | 1051 | 2026-06-10 | 1000 | NULL | Supplier payments | 0 | 232434.66 |
| 149 | 1052 | 2026-06-12 | 6400 | 3100 | Ads | 5100 | 0 |
| 150 | 1052 | 2026-06-12 | 2000 | NULL | Ads | 0 | 5100 |
| 151 | 1053 | 2026-06-20 | 6200 | 2000 | Electricity and gas | 3998.69 | 0 |
| 152 | 1053 | 2026-06-20 | 2000 | NULL | Electricity and gas | 0 | 3998.69 |
| 153 | 1054 | 2026-06-22 | 6600 | NULL | Machine service | 3960 | 0 |
| 154 | 1054 | 2026-06-22 | 2000 | NULL | Machine service | 0 | 3960 |
| 155 | 1055 | 2026-06-25 | 6300 | NULL | Sales travel expenses | 1742.3 | 0 |
| 156 | 1055 | 2026-06-25 | 1000 | NULL | Sales travel expenses | 0 | 1742.3 |
| 157 | 1056 | 2026-06-28 | 6000 | 1000 | Payroll | 14454.64 | 0 |
| 158 | 1056 | 2026-06-28 | 6000 | 1100 | Payroll | 11163.23 | 0 |
| 159 | 1056 | 2026-06-28 | 6000 | 1200 | Payroll | 7807.76 | 0 |
| 160 | 1056 | 2026-06-28 | 6000 | 2000 | Payroll | 24532.24 | 0 |
| 161 | 1056 | 2026-06-28 | 6000 | 2100 | Payroll | 19864.73 | 0 |
| 162 | 1056 | 2026-06-28 | 6000 | 2200 | Payroll | 9885.93 | 0 |
| 163 | 1056 | 2026-06-28 | 6000 | 3000 | Payroll | 13419.99 | 0 |
| 164 | 1056 | 2026-06-28 | 6000 | 3100 | Payroll | 6874.02 | 0 |
| 165 | 1056 | 2026-06-28 | 1000 | NULL | Payroll | 0 | 108002.54 |
| 166 | 1057 | 2026-06-30 | 1100 | NULL | Sales invoices | 293910.52 | 0 |
| 167 | 1057 | 2026-06-30 | 4000 | NULL | Sales invoices | 0 | 293910.52 |
| 168 | 1058 | 2026-06-30 | 1000 | NULL | Customer receipts | 416664.2 | 0 |
| 169 | 1058 | 2026-06-30 | 1100 | NULL | Customer receipts | 0 | 416664.2 |
Step 1: attach ledger and budget to the cost center
Start from cost_centers and join budget and ledger on cost_center_id. Only cost lines carry a cost center, so bank, receivables and revenue lines drop out of the join on their own. The budget joins on the primary key, so there's exactly one budget row per cost center.
Step 2: filter to March and group
WHERE l.entry_date BETWEEN '2026-03-01' AND '2026-03-31' keeps only March. Dates are stored as text in YYYY-MM-DD order, so comparing them works just like comparing numbers. Then GROUP BY c.cost_center_id. You can show b.mar without SUM because it's the same on every row of the group.
Step 3: the variance
Variance is SUM(l.debit - l.credit) - b.mar. For costs, a positive number means over budget. In March, IT, Sales and above all Marketing stand out, while Assembly comes in under.
Task 2: half-year with percentage and flag
For the half-year you need the January to June budget, b.jan + b.feb + b.mar + b.apr + b.may + b.jun, and actuals from every ledger line, which only run to June anyway. Three more columns follow from those two. To avoid writing the variance out again and again, work out budget and actual in a CTE first and build on top of it.
| Column | Calculation | Idea |
|---|---|---|
| variance | actual - budget | positive means overspend |
| variance_pct | ROUND(100 * (actual - budget) / budget, 1) | budget is the base |
| status | CASE WHEN ABS(…) > 5000 AND ABS(…) > 0.1 * budget THEN 'review' ELSE 'ok' END | both thresholds, up or down |
Task For the half-year report, management wants per cost center: cost_center, budget (January to June budget), actual (all costs in the ledger, the data runs to June), variance (actual minus budget), variance_pct (variance as a percentage of budget, rounded to one decimal place) and status: 'review' if the variance is both more than 10% and more than 5000, up or down, otherwise 'ok'. Sorted by variance, largest first.
cost_centers 8 rows, 4 columns
| cost_center_id | name | department | manager |
|---|---|---|---|
| 1000 | Management | Administration | Laura Bennett |
| 1100 | Finance and HR | Administration | Daniel Price |
| 1200 | IT | Administration | Ryan Cooper |
| 2000 | Manufacturing | Operations | Mark Ellis |
| 2100 | Assembly | Operations | Sofia Ramirez |
| 2200 | Warehouse and shipping | Operations | Kevin Shaw |
| 3000 | Sales | Sales | James Turner |
| 3100 | Marketing | Sales | Emily Ward |
budget 8 rows, 13 columns
| cost_center_id | jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1000 | 19300 | 18800 | 19700 | 19300 | 18700 | 18800 | 19600 | 19700 | 18700 | 19000 | 19300 | 19000 |
| 1100 | 11500 | 11500 | 11500 | 11200 | 11300 | 11400 | 11500 | 11300 | 11000 | 11300 | 11000 | 11300 |
| 1200 | 12700 | 8100 | 9300 | 12700 | 8100 | 10200 | 10100 | 10100 | 10300 | 10100 | 10600 | 10100 |
| 2000 | 92900 | 107800 | 146000 | 173100 | 159400 | 140400 | 135500 | 135100 | 141600 | 141100 | 139100 | 138400 |
| 2100 | 56400 | 63800 | 91200 | 99400 | 94600 | 87200 | 83700 | 86400 | 86300 | 82200 | 84500 | 82900 |
| 2200 | 9900 | 10200 | 9900 | 10300 | 10100 | 10100 | 9600 | 10200 | 10100 | 9700 | 9900 | 9700 |
| 3000 | 13100 | 14500 | 14600 | 15800 | 13100 | 13900 | 14900 | 14800 | 15000 | 14700 | 14300 | 14300 |
| 3100 | 8900 | 10900 | 18900 | 18900 | 12900 | 12900 | 15400 | 15500 | 15600 | 15900 | 16300 | 15500 |
ledger 169 rows, 8 columns
| entry_id | doc_no | entry_date | account_id | cost_center_id | description | debit | credit |
|---|---|---|---|---|---|---|---|
| 1 | 1001 | 2026-01-01 | 1000 | NULL | Opening balance bank | 320000 | 0 |
| 2 | 1001 | 2026-01-01 | 3000 | NULL | Opening balance bank | 0 | 320000 |
| 3 | 1002 | 2026-01-03 | 6100 | 2000 | Rent January | 6800 | 0 |
| 4 | 1002 | 2026-01-03 | 6100 | 1000 | Rent January | 4600 | 0 |
| 5 | 1002 | 2026-01-03 | 1000 | NULL | Rent January | 0 | 11400 |
| 6 | 1003 | 2026-01-05 | 6500 | 1200 | Software licences quarter | 4860 | 0 |
| 7 | 1003 | 2026-01-05 | 2000 | NULL | Software licences quarter | 0 | 4860 |
| 8 | 1004 | 2026-01-08 | 5000 | 2000 | Materials purchase | 56177.71 | 0 |
| 9 | 1004 | 2026-01-08 | 5000 | 2100 | Materials purchase | 34355.83 | 0 |
| 10 | 1004 | 2026-01-08 | 2000 | NULL | Materials purchase | 0 | 90533.54 |
| 11 | 1005 | 2026-01-20 | 6200 | 2000 | Electricity and gas | 3416.46 | 0 |
| 12 | 1005 | 2026-01-20 | 2000 | NULL | Electricity and gas | 0 | 3416.46 |
| 13 | 1006 | 2026-01-28 | 6000 | 1000 | Payroll | 14469.07 | 0 |
| 14 | 1006 | 2026-01-28 | 6000 | 1100 | Payroll | 11248.67 | 0 |
| 15 | 1006 | 2026-01-28 | 6000 | 1200 | Payroll | 7764.75 | 0 |
| 16 | 1006 | 2026-01-28 | 6000 | 2000 | Payroll | 24648.2 | 0 |
| 17 | 1006 | 2026-01-28 | 6000 | 2100 | Payroll | 19865.04 | 0 |
| 18 | 1006 | 2026-01-28 | 6000 | 2200 | Payroll | 9919.49 | 0 |
| 19 | 1006 | 2026-01-28 | 6000 | 3000 | Payroll | 13339.13 | 0 |
| 20 | 1006 | 2026-01-28 | 6000 | 3100 | Payroll | 6905.66 | 0 |
| 21 | 1006 | 2026-01-28 | 1000 | NULL | Payroll | 0 | 108160.01 |
| 22 | 1007 | 2026-01-31 | 1100 | NULL | Sales invoices | 190302.72 | 0 |
| 23 | 1007 | 2026-01-31 | 4000 | NULL | Sales invoices | 0 | 190302.72 |
| 24 | 1008 | 2026-01-31 | 1000 | NULL | Customer receipts | 41681.77 | 0 |
| 25 | 1008 | 2026-01-31 | 1100 | NULL | Customer receipts | 0 | 41681.77 |
| 26 | 1009 | 2026-02-03 | 6100 | 2000 | Rent February | 6800 | 0 |
| 27 | 1009 | 2026-02-03 | 6100 | 1000 | Rent February | 4600 | 0 |
| 28 | 1009 | 2026-02-03 | 1000 | NULL | Rent February | 0 | 11400 |
| 29 | 1010 | 2026-02-08 | 5000 | 2000 | Materials purchase | 73668.51 | 0 |
| 30 | 1010 | 2026-02-08 | 5000 | 2100 | Materials purchase | 44273.2 | 0 |
| 31 | 1010 | 2026-02-08 | 2000 | NULL | Materials purchase | 0 | 117941.71 |
| 32 | 1011 | 2026-02-10 | 2000 | NULL | Supplier payments | 98810 | 0 |
| 33 | 1011 | 2026-02-10 | 1000 | NULL | Supplier payments | 0 | 98810 |
| 34 | 1012 | 2026-02-12 | 6400 | 3100 | Ads | 4200 | 0 |
| 35 | 1012 | 2026-02-12 | 2000 | NULL | Ads | 0 | 4200 |
| 36 | 1013 | 2026-02-20 | 6200 | 2000 | Electricity and gas | 3671.31 | 0 |
| 37 | 1013 | 2026-02-20 | 2000 | NULL | Electricity and gas | 0 | 3671.31 |
| 38 | 1014 | 2026-02-25 | 6300 | 3000 | Sales travel expenses | 1286.4 | 0 |
| 39 | 1014 | 2026-02-25 | 1000 | NULL | Sales travel expenses | 0 | 1286.4 |
| 40 | 1015 | 2026-02-28 | 6000 | 1000 | Payroll | 14564.78 | 0 |
| 41 | 1015 | 2026-02-28 | 6000 | 1100 | Payroll | 11248.62 | 0 |
| 42 | 1015 | 2026-02-28 | 6000 | 1200 | Payroll | 7830.19 | 0 |
| 43 | 1015 | 2026-02-28 | 6000 | 2000 | Payroll | 24669.51 | 0 |
| 44 | 1015 | 2026-02-28 | 6000 | 2100 | Payroll | 19869.44 | 0 |
| 45 | 1015 | 2026-02-28 | 6000 | 2200 | Payroll | 9893.72 | 0 |
| 46 | 1015 | 2026-02-28 | 6000 | 3000 | Payroll | 13366.23 | 0 |
| 47 | 1015 | 2026-02-28 | 6000 | 3100 | Payroll | 6921.81 | 0 |
| 48 | 1015 | 2026-02-28 | 1000 | NULL | Payroll | 0 | 108364.3 |
| 49 | 1016 | 2026-02-28 | 1100 | NULL | Sales invoices | 249339.75 | 0 |
| 50 | 1016 | 2026-02-28 | 4000 | NULL | Sales invoices | 0 | 249339.75 |
| 51 | 1017 | 2026-02-28 | 1000 | NULL | Customer receipts | 77712.92 | 0 |
| 52 | 1017 | 2026-02-28 | 1100 | NULL | Customer receipts | 0 | 77712.92 |
| 53 | 1018 | 2026-03-03 | 6100 | 2000 | Rent March | 6800 | 0 |
| 54 | 1018 | 2026-03-03 | 6100 | 1000 | Rent March | 4600 | 0 |
| 55 | 1018 | 2026-03-03 | 1000 | NULL | Rent March | 0 | 11400 |
| 56 | 1019 | 2026-03-05 | 6500 | 1200 | Laptop hardware | 3290 | 0 |
| 57 | 1019 | 2026-03-05 | 2000 | NULL | Laptop hardware | 0 | 3290 |
| 58 | 1020 | 2026-03-08 | 5000 | 2000 | Materials purchase | 110523.12 | 0 |
| 59 | 1020 | 2026-03-08 | 5000 | 2100 | Materials purchase | 67928.04 | 0 |
| 60 | 1020 | 2026-03-08 | 2000 | NULL | Materials purchase | 0 | 178451.16 |
| 61 | 1021 | 2026-03-10 | 2000 | NULL | Supplier payments | 125813.02 | 0 |
| 62 | 1021 | 2026-03-10 | 1000 | NULL | Supplier payments | 0 | 125813.02 |
| 63 | 1022 | 2026-03-12 | 6400 | 3100 | Spring campaign | 18600 | 0 |
| 64 | 1022 | 2026-03-12 | 2000 | NULL | Spring campaign | 0 | 18600 |
| 65 | 1023 | 2026-03-20 | 6200 | 2000 | Electricity and gas | 3929.91 | 0 |
| 66 | 1023 | 2026-03-20 | 2000 | NULL | Electricity and gas | 0 | 3929.91 |
| 67 | 1024 | 2026-03-22 | 6600 | 2000 | Machine service | 2480 | 0 |
| 68 | 1024 | 2026-03-22 | 2000 | NULL | Machine service | 0 | 2480 |
| 69 | 1025 | 2026-03-26 | 2000 | NULL | Supplier credit note return | 1240 | 0 |
| 70 | 1025 | 2026-03-26 | 5000 | 2000 | Supplier credit note return | 0 | 1240 |
| 71 | 1026 | 2026-03-28 | 6000 | 1000 | Payroll | 14521.48 | 0 |
| 72 | 1026 | 2026-03-28 | 6000 | 1100 | Payroll | 11254.45 | 0 |
| 73 | 1026 | 2026-03-28 | 6000 | 1200 | Payroll | 7770.97 | 0 |
| 74 | 1026 | 2026-03-28 | 6000 | 2000 | Payroll | 24522.86 | 0 |
| 75 | 1026 | 2026-03-28 | 6000 | 2100 | Payroll | 19761.5 | 0 |
| 76 | 1026 | 2026-03-28 | 6000 | 2200 | Payroll | 9898.42 | 0 |
| 77 | 1026 | 2026-03-28 | 6000 | 3000 | Payroll | 16593.37 | 0 |
| 78 | 1026 | 2026-03-28 | 6000 | 3100 | Payroll | 6919.28 | 0 |
| 79 | 1026 | 2026-03-28 | 1000 | NULL | Payroll | 0 | 111242.33 |
| 80 | 1027 | 2026-03-31 | 1100 | NULL | Sales invoices | 375915.93 | 0 |
| 81 | 1027 | 2026-03-31 | 4000 | NULL | Sales invoices | 0 | 375915.93 |
| 82 | 1028 | 2026-03-31 | 1000 | NULL | Customer receipts | 297540.71 | 0 |
| 83 | 1028 | 2026-03-31 | 1100 | NULL | Customer receipts | 0 | 297540.71 |
| 84 | 1029 | 2026-04-03 | 6100 | 2000 | Rent April | 6800 | 0 |
| 85 | 1029 | 2026-04-03 | 6100 | 1000 | Rent April | 4600 | 0 |
| 86 | 1029 | 2026-04-03 | 1000 | NULL | Rent April | 0 | 11400 |
| 87 | 1030 | 2026-04-05 | 6500 | 1200 | Software licences quarter | 4860 | 0 |
| 88 | 1030 | 2026-04-05 | 2000 | NULL | Software licences quarter | 0 | 4860 |
| 89 | 1031 | 2026-04-08 | 5000 | 2000 | Materials purchase | 132153.91 | 0 |
| 90 | 1031 | 2026-04-08 | 5000 | 2100 | Materials purchase | 84705.12 | 0 |
| 91 | 1031 | 2026-04-08 | 2000 | NULL | Materials purchase | 0 | 216859.03 |
| 92 | 1032 | 2026-04-10 | 2000 | NULL | Supplier payments | 205511.07 | 0 |
| 93 | 1032 | 2026-04-10 | 1000 | NULL | Supplier payments | 0 | 205511.07 |
| 94 | 1033 | 2026-04-12 | 6400 | 3100 | Spring campaign | 21400 | 0 |
| 95 | 1033 | 2026-04-12 | 2000 | NULL | Spring campaign | 0 | 21400 |
| 96 | 1034 | 2026-04-20 | 6200 | 2000 | Electricity and gas | 4275.47 | 0 |
| 97 | 1034 | 2026-04-20 | 2000 | NULL | Electricity and gas | 0 | 4275.47 |
| 98 | 1035 | 2026-04-25 | 6300 | 3000 | Sales travel expenses | 2914.7 | 0 |
| 99 | 1035 | 2026-04-25 | 1000 | NULL | Sales travel expenses | 0 | 2914.7 |
| 100 | 1036 | 2026-04-28 | 6000 | 1000 | Payroll | 14511.12 | 0 |
| 101 | 1036 | 2026-04-28 | 6000 | 1100 | Payroll | 11250.53 | 0 |
| 102 | 1036 | 2026-04-28 | 6000 | 1200 | Payroll | 7799.38 | 0 |
| 103 | 1036 | 2026-04-28 | 6000 | 2000 | Payroll | 24704.1 | 0 |
| 104 | 1036 | 2026-04-28 | 6000 | 2100 | Payroll | 22185.09 | 0 |
| 105 | 1036 | 2026-04-28 | 6000 | 2200 | Payroll | 9945.03 | 0 |
| 106 | 1036 | 2026-04-28 | 6000 | 3000 | Payroll | 13372.53 | 0 |
| 107 | 1036 | 2026-04-28 | 6000 | 3100 | Payroll | 6882.32 | 0 |
| 108 | 1036 | 2026-04-28 | 1000 | NULL | Payroll | 0 | 110650.1 |
| 109 | 1037 | 2026-04-30 | 1100 | NULL | Sales invoices | 452137.92 | 0 |
| 110 | 1037 | 2026-04-30 | 4000 | NULL | Sales invoices | 0 | 452137.92 |
| 111 | 1038 | 2026-04-30 | 1000 | NULL | Customer receipts | 235601.59 | 0 |
| 112 | 1038 | 2026-04-30 | 1100 | NULL | Customer receipts | 0 | 235601.59 |
| 113 | 1039 | 2026-05-03 | 6100 | 2000 | Rent May | 6800 | 0 |
| 114 | 1039 | 2026-05-03 | 6100 | 1000 | Rent May | 4600 | 0 |
| 115 | 1039 | 2026-05-03 | 1000 | NULL | Rent May | 0 | 11400 |
| 116 | 1040 | 2026-05-08 | 5000 | 2000 | Materials purchase | 141737.72 | 0 |
| 117 | 1040 | 2026-05-08 | 5000 | 2100 | Materials purchase | 82231.25 | 0 |
| 118 | 1040 | 2026-05-08 | 2000 | NULL | Materials purchase | 0 | 223968.97 |
| 119 | 1041 | 2026-05-10 | 2000 | NULL | Supplier payments | 247394.5 | 0 |
| 120 | 1041 | 2026-05-10 | 1000 | NULL | Supplier payments | 0 | 247394.5 |
| 121 | 1042 | 2026-05-12 | 6400 | 3100 | Ads | 6300 | 0 |
| 122 | 1042 | 2026-05-12 | 2000 | NULL | Ads | 0 | 6300 |
| 123 | 1043 | 2026-05-20 | 6200 | 2000 | Electricity and gas | 4015.69 | 0 |
| 124 | 1043 | 2026-05-20 | 2000 | NULL | Electricity and gas | 0 | 4015.69 |
| 125 | 1044 | 2026-05-27 | 2000 | NULL | Agency credit note | 1850 | 0 |
| 126 | 1044 | 2026-05-27 | 6400 | 3100 | Agency credit note | 0 | 1850 |
| 127 | 1045 | 2026-05-28 | 6000 | 1000 | Payroll | 14460.25 | 0 |
| 128 | 1045 | 2026-05-28 | 6000 | 1100 | Payroll | 11220.17 | 0 |
| 129 | 1045 | 2026-05-28 | 6000 | 1200 | Payroll | 7803.77 | 0 |
| 130 | 1045 | 2026-05-28 | 6000 | 2000 | Payroll | 24549.87 | 0 |
| 131 | 1045 | 2026-05-28 | 6000 | 2100 | Payroll | 22845.09 | 0 |
| 132 | 1045 | 2026-05-28 | 6000 | 2200 | Payroll | 9873.46 | 0 |
| 133 | 1045 | 2026-05-28 | 6000 | 3000 | Payroll | 13377.78 | 0 |
| 134 | 1045 | 2026-05-28 | 6000 | 3100 | Payroll | 6876.77 | 0 |
| 135 | 1045 | 2026-05-28 | 1000 | NULL | Payroll | 0 | 111007.16 |
| 136 | 1046 | 2026-05-31 | 1100 | NULL | Sales invoices | 437876.58 | 0 |
| 137 | 1046 | 2026-05-31 | 4000 | NULL | Sales invoices | 0 | 437876.58 |
| 138 | 1047 | 2026-05-31 | 1000 | NULL | Customer receipts | 396656.49 | 0 |
| 139 | 1047 | 2026-05-31 | 1100 | NULL | Customer receipts | 0 | 396656.49 |
| 140 | 1048 | 2026-06-03 | 6100 | 2000 | Rent June | 6800 | 0 |
| 141 | 1048 | 2026-06-03 | 6100 | 1000 | Rent June | 4600 | 0 |
| 142 | 1048 | 2026-06-03 | 1000 | NULL | Rent June | 0 | 11400 |
| 143 | 1049 | 2026-06-05 | 6500 | 1200 | Laptop hardware | 2140 | 0 |
| 144 | 1050 | 2026-06-08 | 5000 | 2000 | Materials purchase | 100972.77 | 0 |
| 145 | 1050 | 2026-06-08 | 5000 | 2100 | Materials purchase | 65827.08 | 0 |
| 146 | 1050 | 2026-06-08 | 2000 | NULL | Materials purchase | 0 | 166529.85 |
| 147 | 1051 | 2026-06-10 | 2000 | NULL | Supplier payments | 232434.66 | 0 |
| 148 | 1051 | 2026-06-10 | 1000 | NULL | Supplier payments | 0 | 232434.66 |
| 149 | 1052 | 2026-06-12 | 6400 | 3100 | Ads | 5100 | 0 |
| 150 | 1052 | 2026-06-12 | 2000 | NULL | Ads | 0 | 5100 |
| 151 | 1053 | 2026-06-20 | 6200 | 2000 | Electricity and gas | 3998.69 | 0 |
| 152 | 1053 | 2026-06-20 | 2000 | NULL | Electricity and gas | 0 | 3998.69 |
| 153 | 1054 | 2026-06-22 | 6600 | NULL | Machine service | 3960 | 0 |
| 154 | 1054 | 2026-06-22 | 2000 | NULL | Machine service | 0 | 3960 |
| 155 | 1055 | 2026-06-25 | 6300 | NULL | Sales travel expenses | 1742.3 | 0 |
| 156 | 1055 | 2026-06-25 | 1000 | NULL | Sales travel expenses | 0 | 1742.3 |
| 157 | 1056 | 2026-06-28 | 6000 | 1000 | Payroll | 14454.64 | 0 |
| 158 | 1056 | 2026-06-28 | 6000 | 1100 | Payroll | 11163.23 | 0 |
| 159 | 1056 | 2026-06-28 | 6000 | 1200 | Payroll | 7807.76 | 0 |
| 160 | 1056 | 2026-06-28 | 6000 | 2000 | Payroll | 24532.24 | 0 |
| 161 | 1056 | 2026-06-28 | 6000 | 2100 | Payroll | 19864.73 | 0 |
| 162 | 1056 | 2026-06-28 | 6000 | 2200 | Payroll | 9885.93 | 0 |
| 163 | 1056 | 2026-06-28 | 6000 | 3000 | Payroll | 13419.99 | 0 |
| 164 | 1056 | 2026-06-28 | 6000 | 3100 | Payroll | 6874.02 | 0 |
| 165 | 1056 | 2026-06-28 | 1000 | NULL | Payroll | 0 | 108002.54 |
| 166 | 1057 | 2026-06-30 | 1100 | NULL | Sales invoices | 293910.52 | 0 |
| 167 | 1057 | 2026-06-30 | 4000 | NULL | Sales invoices | 0 | 293910.52 |
| 168 | 1058 | 2026-06-30 | 1000 | NULL | Customer receipts | 416664.2 | 0 |
| 169 | 1058 | 2026-06-30 | 1100 | NULL | Customer receipts | 0 | 416664.2 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Why two thresholds? A percentage alone flags a few hundred in a small cost center, and an amount alone flags every rounding difference in Manufacturing. Only both together separate signal from noise. ABS makes sure big underspends get flagged too; they often mean an invoice is missing or was coded to the wrong place. More on CASE in the CASE WHEN lesson.
Common mistakes
Summing debits only
SUM(l.debit) ignores credit notes like the March return. Columns and rows are right, but the exercise says: "Columns and row count match, but not every value."
Forgetting the date filter
Without the March filter you compare six months of actuals with one month of budget, and Manufacturing seems hundreds of thousands over. Again only columns and rows match.
Using the full-year budget
Add all twelve month columns for the half-year and you compare actuals to June with the annual budget. Every cost center then looks massively under budget.
Percentage on actual
(actual - budget) / actual gives a different number. In a budget variance, budget is the base.
Flag without ABS
CASE WHEN actual - budget > 5000 … only catches overspends. The visible data doesn't show it, but the hidden check data does, and the exercise then tells you the result only works with this data.
Check your result
- Task 1: Marketing is 6,619.28 over its March budget of 18,900, Assembly 3,510.46 under, and Warehouse almost exactly on budget.
- Task 2: only Marketing is flagged "review", at 14.1%. Assembly is roughly 11,000 over, but that's only 2.3%.
- The actual column in task 2 must add up to all cost lines that carry a cost center. Lines without one are missing from every cost center report; case 6, month-end close, shows how to find them.
The same report as a workbook is in the Excel budget vs actual case.
Back to the course overview.