Case 4: cost center analysis in SQL
Where does the money go? You spread the half-year costs over cost centers and departments, work out their shares and find, for each cost center, the cost type to raise first in the review with its budget holder.
In short: first costs per cost center with GROUP BY, then the shares with window functions. SUM(costs) OVER () writes the grand total on every row, SUM(costs) OVER (PARTITION BY department) the department total. The largest cost type per cost center comes from ROW_NUMBER() OVER (PARTITION BY … ORDER BY … DESC), filtered to number 1.
The situation
Cost center reviews are due in July. Every budget holder gets a page with their costs, and management wants to see beforehand how the half-year costs split across the eight cost centers and the three departments, Administration, Operations and Sales. For each review it should also be clear which cost type weighs most in that cost center.
The tables
- ledger: journal lines with account_id, cost_center_id, debit and credit. Costs are
debit - credit. - cost_centers: name and department of each cost center.
- accounts: the name of every account. The expense accounts are the cost types: materials, salaries, rent, energy and so on.
GROUP BY on its own gives you costs per department, but the individual cost centers disappear:
SELECT c.department, SUM(l.debit - l.credit) AS costs
FROM ledger l
JOIN cost_centers c ON c.cost_center_id = l.cost_center_id
GROUP BY c.department;
| department | costs |
|---|---|
| Administration | 243893.83 |
| Operations | 1391335.51 |
| Sales | 182799.99 |
For shares you need both on the same row: the cost center's own costs and its department's total. That's exactly what window functions do.
Task 1: share of total and of department
Task How are the half-year costs spread? For each cost center show: cost_center, department, costs (debit minus credit), share_total (share of all costs in percent) and share_department (share of its own department's costs in percent), both rounded to one decimal place.
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 |
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 |
Step 1: costs per cost center
A CTE works out name, department and SUM(l.debit - l.credit) per cost center. That gives eight rows, one per cost center.
Step 2: two totals across rows
The window functions work on those eight rows. SUM(costs) OVER () with empty brackets adds up all rows and writes the result on each one. SUM(costs) OVER (PARTITION BY department) only adds up within the department. Divide each row's costs by the right total, multiply by 100, and you have both shares. The running totals and shares lesson goes deeper into sums over windows.
It also works without a CTE: SUM(SUM(l.debit - l.credit)) OVER () is allowed, because the database runs GROUP BY first and the window function afterwards. The CTE is just easier to read.
Task 2: the largest cost type per cost center
Now you group more finely, by cost center and account, which gives 17 rows. In each cost center you only want the one with the highest cost. That's the classic "top 1 per group" pattern: number, then filter.
ROW_NUMBER() OVER (PARTITION BY c.cost_center_id
ORDER BY SUM(l.debit - l.credit) DESC) AS rn
PARTITION BY restarts the count at 1 in every cost center, and ORDER BY … DESC gives number 1 to the most expensive cost type. You can only filter one level further out, with WHERE rn = 1.
Task For the review meetings with budget holders, management wants the largest cost type for each cost center. Show cost_center, account (the account name) and costs (debit minus credit for that account in that cost center over the half-year), one row per cost center.
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 |
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 |
accounts 13 rows, 3 columns
| account_id | name | type |
|---|---|---|
| 1000 | Bank | asset |
| 1100 | Accounts receivable | asset |
| 2000 | Accounts payable | liability |
| 3000 | Equity | equity |
| 4000 | Sales revenue | revenue |
| 5000 | Materials | expense |
| 6000 | Salaries and wages | expense |
| 6100 | Rent and occupancy | expense |
| 6200 | Energy | expense |
| 6300 | Travel | expense |
| 6400 | Advertising | expense |
| 6500 | IT and software | expense |
| 6600 | Repairs and maintenance | expense |
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.
The result is typical for a manufacturer: materials lead in Manufacturing and Assembly, advertising in Marketing, and payroll everywhere else. The review gets interesting at the second and third cost type; rn <= 3 instead of rn = 1 gives you those.
Common mistakes
Forgetting the 100
ROUND(costs / SUM(costs) OVER (), 1) gives 0.5 instead of 45.6. The exercise reports that the column and row count match but the values don't.
ORDER BY instead of PARTITION BY
SUM(costs) OVER (ORDER BY department) is a running total across departments, not a total per department. The department shares are then only right for the first department and too small after that.
Columns swapped
If the department share comes before the total share, the exercise says: "The columns are right, but not their order."
Forgetting PARTITION BY
Without PARTITION BY, ROW_NUMBER numbers every row in one sequence and there's only a single number 1. The exercise says: "Your query returns only 1 row. Wanted: 8 rows."
Forgetting DESC
Without DESC the smallest cost type gets number 1, so you find the least important cost type instead of the most important.
Filtering on the number at the same level
WHERE rn = 1 at the same level as ROW_NUMBER doesn't work, because WHERE runs before the window function. The exercise explains that window functions are computed in a CTE first and filtered afterwards.
Check your result
- Task 1: Manufacturing carries 45.6% of all costs and 59.5% of Operations. The total shares add up to 100, and so do the department shares within each department, give or take rounding.
- Task 2: eight rows, one per cost center. Manufacturing's materials come to 613,993.74.
- Costs without a cost center are missing from both reports. Whether there are any, you check in case 6, month-end close.
Back to the course overview.