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;
departmentcosts
Administration243893.83
Operations1391335.51
Sales182799.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_iddoc_noentry_dateaccount_idcost_center_iddescriptiondebitcredit
110012026-01-011000NULLOpening balance bank3200000
210012026-01-013000NULLOpening balance bank0320000
310022026-01-0361002000Rent January68000
410022026-01-0361001000Rent January46000
510022026-01-031000NULLRent January011400
610032026-01-0565001200Software licences quarter48600
710032026-01-052000NULLSoftware licences quarter04860
810042026-01-0850002000Materials purchase56177.710
910042026-01-0850002100Materials purchase34355.830
1010042026-01-082000NULLMaterials purchase090533.54
1110052026-01-2062002000Electricity and gas3416.460
1210052026-01-202000NULLElectricity and gas03416.46
1310062026-01-2860001000Payroll14469.070
1410062026-01-2860001100Payroll11248.670
1510062026-01-2860001200Payroll7764.750
1610062026-01-2860002000Payroll24648.20
1710062026-01-2860002100Payroll19865.040
1810062026-01-2860002200Payroll9919.490
1910062026-01-2860003000Payroll13339.130
2010062026-01-2860003100Payroll6905.660
2110062026-01-281000NULLPayroll0108160.01
2210072026-01-311100NULLSales invoices190302.720
2310072026-01-314000NULLSales invoices0190302.72
2410082026-01-311000NULLCustomer receipts41681.770
2510082026-01-311100NULLCustomer receipts041681.77
2610092026-02-0361002000Rent February68000
2710092026-02-0361001000Rent February46000
2810092026-02-031000NULLRent February011400
2910102026-02-0850002000Materials purchase73668.510
3010102026-02-0850002100Materials purchase44273.20
3110102026-02-082000NULLMaterials purchase0117941.71
3210112026-02-102000NULLSupplier payments988100
3310112026-02-101000NULLSupplier payments098810
3410122026-02-1264003100Ads42000
3510122026-02-122000NULLAds04200
3610132026-02-2062002000Electricity and gas3671.310
3710132026-02-202000NULLElectricity and gas03671.31
3810142026-02-2563003000Sales travel expenses1286.40
3910142026-02-251000NULLSales travel expenses01286.4
4010152026-02-2860001000Payroll14564.780
4110152026-02-2860001100Payroll11248.620
4210152026-02-2860001200Payroll7830.190
4310152026-02-2860002000Payroll24669.510
4410152026-02-2860002100Payroll19869.440
4510152026-02-2860002200Payroll9893.720
4610152026-02-2860003000Payroll13366.230
4710152026-02-2860003100Payroll6921.810
4810152026-02-281000NULLPayroll0108364.3
4910162026-02-281100NULLSales invoices249339.750
5010162026-02-284000NULLSales invoices0249339.75
5110172026-02-281000NULLCustomer receipts77712.920
5210172026-02-281100NULLCustomer receipts077712.92
5310182026-03-0361002000Rent March68000
5410182026-03-0361001000Rent March46000
5510182026-03-031000NULLRent March011400
5610192026-03-0565001200Laptop hardware32900
5710192026-03-052000NULLLaptop hardware03290
5810202026-03-0850002000Materials purchase110523.120
5910202026-03-0850002100Materials purchase67928.040
6010202026-03-082000NULLMaterials purchase0178451.16
6110212026-03-102000NULLSupplier payments125813.020
6210212026-03-101000NULLSupplier payments0125813.02
6310222026-03-1264003100Spring campaign186000
6410222026-03-122000NULLSpring campaign018600
6510232026-03-2062002000Electricity and gas3929.910
6610232026-03-202000NULLElectricity and gas03929.91
6710242026-03-2266002000Machine service24800
6810242026-03-222000NULLMachine service02480
6910252026-03-262000NULLSupplier credit note return12400
7010252026-03-2650002000Supplier credit note return01240
7110262026-03-2860001000Payroll14521.480
7210262026-03-2860001100Payroll11254.450
7310262026-03-2860001200Payroll7770.970
7410262026-03-2860002000Payroll24522.860
7510262026-03-2860002100Payroll19761.50
7610262026-03-2860002200Payroll9898.420
7710262026-03-2860003000Payroll16593.370
7810262026-03-2860003100Payroll6919.280
7910262026-03-281000NULLPayroll0111242.33
8010272026-03-311100NULLSales invoices375915.930
8110272026-03-314000NULLSales invoices0375915.93
8210282026-03-311000NULLCustomer receipts297540.710
8310282026-03-311100NULLCustomer receipts0297540.71
8410292026-04-0361002000Rent April68000
8510292026-04-0361001000Rent April46000
8610292026-04-031000NULLRent April011400
8710302026-04-0565001200Software licences quarter48600
8810302026-04-052000NULLSoftware licences quarter04860
8910312026-04-0850002000Materials purchase132153.910
9010312026-04-0850002100Materials purchase84705.120
9110312026-04-082000NULLMaterials purchase0216859.03
9210322026-04-102000NULLSupplier payments205511.070
9310322026-04-101000NULLSupplier payments0205511.07
9410332026-04-1264003100Spring campaign214000
9510332026-04-122000NULLSpring campaign021400
9610342026-04-2062002000Electricity and gas4275.470
9710342026-04-202000NULLElectricity and gas04275.47
9810352026-04-2563003000Sales travel expenses2914.70
9910352026-04-251000NULLSales travel expenses02914.7
10010362026-04-2860001000Payroll14511.120
10110362026-04-2860001100Payroll11250.530
10210362026-04-2860001200Payroll7799.380
10310362026-04-2860002000Payroll24704.10
10410362026-04-2860002100Payroll22185.090
10510362026-04-2860002200Payroll9945.030
10610362026-04-2860003000Payroll13372.530
10710362026-04-2860003100Payroll6882.320
10810362026-04-281000NULLPayroll0110650.1
10910372026-04-301100NULLSales invoices452137.920
11010372026-04-304000NULLSales invoices0452137.92
11110382026-04-301000NULLCustomer receipts235601.590
11210382026-04-301100NULLCustomer receipts0235601.59
11310392026-05-0361002000Rent May68000
11410392026-05-0361001000Rent May46000
11510392026-05-031000NULLRent May011400
11610402026-05-0850002000Materials purchase141737.720
11710402026-05-0850002100Materials purchase82231.250
11810402026-05-082000NULLMaterials purchase0223968.97
11910412026-05-102000NULLSupplier payments247394.50
12010412026-05-101000NULLSupplier payments0247394.5
12110422026-05-1264003100Ads63000
12210422026-05-122000NULLAds06300
12310432026-05-2062002000Electricity and gas4015.690
12410432026-05-202000NULLElectricity and gas04015.69
12510442026-05-272000NULLAgency credit note18500
12610442026-05-2764003100Agency credit note01850
12710452026-05-2860001000Payroll14460.250
12810452026-05-2860001100Payroll11220.170
12910452026-05-2860001200Payroll7803.770
13010452026-05-2860002000Payroll24549.870
13110452026-05-2860002100Payroll22845.090
13210452026-05-2860002200Payroll9873.460
13310452026-05-2860003000Payroll13377.780
13410452026-05-2860003100Payroll6876.770
13510452026-05-281000NULLPayroll0111007.16
13610462026-05-311100NULLSales invoices437876.580
13710462026-05-314000NULLSales invoices0437876.58
13810472026-05-311000NULLCustomer receipts396656.490
13910472026-05-311100NULLCustomer receipts0396656.49
14010482026-06-0361002000Rent June68000
14110482026-06-0361001000Rent June46000
14210482026-06-031000NULLRent June011400
14310492026-06-0565001200Laptop hardware21400
14410502026-06-0850002000Materials purchase100972.770
14510502026-06-0850002100Materials purchase65827.080
14610502026-06-082000NULLMaterials purchase0166529.85
14710512026-06-102000NULLSupplier payments232434.660
14810512026-06-101000NULLSupplier payments0232434.66
14910522026-06-1264003100Ads51000
15010522026-06-122000NULLAds05100
15110532026-06-2062002000Electricity and gas3998.690
15210532026-06-202000NULLElectricity and gas03998.69
15310542026-06-226600NULLMachine service39600
15410542026-06-222000NULLMachine service03960
15510552026-06-256300NULLSales travel expenses1742.30
15610552026-06-251000NULLSales travel expenses01742.3
15710562026-06-2860001000Payroll14454.640
15810562026-06-2860001100Payroll11163.230
15910562026-06-2860001200Payroll7807.760
16010562026-06-2860002000Payroll24532.240
16110562026-06-2860002100Payroll19864.730
16210562026-06-2860002200Payroll9885.930
16310562026-06-2860003000Payroll13419.990
16410562026-06-2860003100Payroll6874.020
16510562026-06-281000NULLPayroll0108002.54
16610572026-06-301100NULLSales invoices293910.520
16710572026-06-304000NULLSales invoices0293910.52
16810582026-06-301000NULLCustomer receipts416664.20
16910582026-06-301100NULLCustomer receipts0416664.2
cost_centers 8 rows, 4 columns
cost_center_idnamedepartmentmanager
1000ManagementAdministrationLaura Bennett
1100Finance and HRAdministrationDaniel Price
1200ITAdministrationRyan Cooper
2000ManufacturingOperationsMark Ellis
2100AssemblyOperationsSofia Ramirez
2200Warehouse and shippingOperationsKevin Shaw
3000SalesSalesJames Turner
3100MarketingSalesEmily 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_iddoc_noentry_dateaccount_idcost_center_iddescriptiondebitcredit
110012026-01-011000NULLOpening balance bank3200000
210012026-01-013000NULLOpening balance bank0320000
310022026-01-0361002000Rent January68000
410022026-01-0361001000Rent January46000
510022026-01-031000NULLRent January011400
610032026-01-0565001200Software licences quarter48600
710032026-01-052000NULLSoftware licences quarter04860
810042026-01-0850002000Materials purchase56177.710
910042026-01-0850002100Materials purchase34355.830
1010042026-01-082000NULLMaterials purchase090533.54
1110052026-01-2062002000Electricity and gas3416.460
1210052026-01-202000NULLElectricity and gas03416.46
1310062026-01-2860001000Payroll14469.070
1410062026-01-2860001100Payroll11248.670
1510062026-01-2860001200Payroll7764.750
1610062026-01-2860002000Payroll24648.20
1710062026-01-2860002100Payroll19865.040
1810062026-01-2860002200Payroll9919.490
1910062026-01-2860003000Payroll13339.130
2010062026-01-2860003100Payroll6905.660
2110062026-01-281000NULLPayroll0108160.01
2210072026-01-311100NULLSales invoices190302.720
2310072026-01-314000NULLSales invoices0190302.72
2410082026-01-311000NULLCustomer receipts41681.770
2510082026-01-311100NULLCustomer receipts041681.77
2610092026-02-0361002000Rent February68000
2710092026-02-0361001000Rent February46000
2810092026-02-031000NULLRent February011400
2910102026-02-0850002000Materials purchase73668.510
3010102026-02-0850002100Materials purchase44273.20
3110102026-02-082000NULLMaterials purchase0117941.71
3210112026-02-102000NULLSupplier payments988100
3310112026-02-101000NULLSupplier payments098810
3410122026-02-1264003100Ads42000
3510122026-02-122000NULLAds04200
3610132026-02-2062002000Electricity and gas3671.310
3710132026-02-202000NULLElectricity and gas03671.31
3810142026-02-2563003000Sales travel expenses1286.40
3910142026-02-251000NULLSales travel expenses01286.4
4010152026-02-2860001000Payroll14564.780
4110152026-02-2860001100Payroll11248.620
4210152026-02-2860001200Payroll7830.190
4310152026-02-2860002000Payroll24669.510
4410152026-02-2860002100Payroll19869.440
4510152026-02-2860002200Payroll9893.720
4610152026-02-2860003000Payroll13366.230
4710152026-02-2860003100Payroll6921.810
4810152026-02-281000NULLPayroll0108364.3
4910162026-02-281100NULLSales invoices249339.750
5010162026-02-284000NULLSales invoices0249339.75
5110172026-02-281000NULLCustomer receipts77712.920
5210172026-02-281100NULLCustomer receipts077712.92
5310182026-03-0361002000Rent March68000
5410182026-03-0361001000Rent March46000
5510182026-03-031000NULLRent March011400
5610192026-03-0565001200Laptop hardware32900
5710192026-03-052000NULLLaptop hardware03290
5810202026-03-0850002000Materials purchase110523.120
5910202026-03-0850002100Materials purchase67928.040
6010202026-03-082000NULLMaterials purchase0178451.16
6110212026-03-102000NULLSupplier payments125813.020
6210212026-03-101000NULLSupplier payments0125813.02
6310222026-03-1264003100Spring campaign186000
6410222026-03-122000NULLSpring campaign018600
6510232026-03-2062002000Electricity and gas3929.910
6610232026-03-202000NULLElectricity and gas03929.91
6710242026-03-2266002000Machine service24800
6810242026-03-222000NULLMachine service02480
6910252026-03-262000NULLSupplier credit note return12400
7010252026-03-2650002000Supplier credit note return01240
7110262026-03-2860001000Payroll14521.480
7210262026-03-2860001100Payroll11254.450
7310262026-03-2860001200Payroll7770.970
7410262026-03-2860002000Payroll24522.860
7510262026-03-2860002100Payroll19761.50
7610262026-03-2860002200Payroll9898.420
7710262026-03-2860003000Payroll16593.370
7810262026-03-2860003100Payroll6919.280
7910262026-03-281000NULLPayroll0111242.33
8010272026-03-311100NULLSales invoices375915.930
8110272026-03-314000NULLSales invoices0375915.93
8210282026-03-311000NULLCustomer receipts297540.710
8310282026-03-311100NULLCustomer receipts0297540.71
8410292026-04-0361002000Rent April68000
8510292026-04-0361001000Rent April46000
8610292026-04-031000NULLRent April011400
8710302026-04-0565001200Software licences quarter48600
8810302026-04-052000NULLSoftware licences quarter04860
8910312026-04-0850002000Materials purchase132153.910
9010312026-04-0850002100Materials purchase84705.120
9110312026-04-082000NULLMaterials purchase0216859.03
9210322026-04-102000NULLSupplier payments205511.070
9310322026-04-101000NULLSupplier payments0205511.07
9410332026-04-1264003100Spring campaign214000
9510332026-04-122000NULLSpring campaign021400
9610342026-04-2062002000Electricity and gas4275.470
9710342026-04-202000NULLElectricity and gas04275.47
9810352026-04-2563003000Sales travel expenses2914.70
9910352026-04-251000NULLSales travel expenses02914.7
10010362026-04-2860001000Payroll14511.120
10110362026-04-2860001100Payroll11250.530
10210362026-04-2860001200Payroll7799.380
10310362026-04-2860002000Payroll24704.10
10410362026-04-2860002100Payroll22185.090
10510362026-04-2860002200Payroll9945.030
10610362026-04-2860003000Payroll13372.530
10710362026-04-2860003100Payroll6882.320
10810362026-04-281000NULLPayroll0110650.1
10910372026-04-301100NULLSales invoices452137.920
11010372026-04-304000NULLSales invoices0452137.92
11110382026-04-301000NULLCustomer receipts235601.590
11210382026-04-301100NULLCustomer receipts0235601.59
11310392026-05-0361002000Rent May68000
11410392026-05-0361001000Rent May46000
11510392026-05-031000NULLRent May011400
11610402026-05-0850002000Materials purchase141737.720
11710402026-05-0850002100Materials purchase82231.250
11810402026-05-082000NULLMaterials purchase0223968.97
11910412026-05-102000NULLSupplier payments247394.50
12010412026-05-101000NULLSupplier payments0247394.5
12110422026-05-1264003100Ads63000
12210422026-05-122000NULLAds06300
12310432026-05-2062002000Electricity and gas4015.690
12410432026-05-202000NULLElectricity and gas04015.69
12510442026-05-272000NULLAgency credit note18500
12610442026-05-2764003100Agency credit note01850
12710452026-05-2860001000Payroll14460.250
12810452026-05-2860001100Payroll11220.170
12910452026-05-2860001200Payroll7803.770
13010452026-05-2860002000Payroll24549.870
13110452026-05-2860002100Payroll22845.090
13210452026-05-2860002200Payroll9873.460
13310452026-05-2860003000Payroll13377.780
13410452026-05-2860003100Payroll6876.770
13510452026-05-281000NULLPayroll0111007.16
13610462026-05-311100NULLSales invoices437876.580
13710462026-05-314000NULLSales invoices0437876.58
13810472026-05-311000NULLCustomer receipts396656.490
13910472026-05-311100NULLCustomer receipts0396656.49
14010482026-06-0361002000Rent June68000
14110482026-06-0361001000Rent June46000
14210482026-06-031000NULLRent June011400
14310492026-06-0565001200Laptop hardware21400
14410502026-06-0850002000Materials purchase100972.770
14510502026-06-0850002100Materials purchase65827.080
14610502026-06-082000NULLMaterials purchase0166529.85
14710512026-06-102000NULLSupplier payments232434.660
14810512026-06-101000NULLSupplier payments0232434.66
14910522026-06-1264003100Ads51000
15010522026-06-122000NULLAds05100
15110532026-06-2062002000Electricity and gas3998.690
15210532026-06-202000NULLElectricity and gas03998.69
15310542026-06-226600NULLMachine service39600
15410542026-06-222000NULLMachine service03960
15510552026-06-256300NULLSales travel expenses1742.30
15610552026-06-251000NULLSales travel expenses01742.3
15710562026-06-2860001000Payroll14454.640
15810562026-06-2860001100Payroll11163.230
15910562026-06-2860001200Payroll7807.760
16010562026-06-2860002000Payroll24532.240
16110562026-06-2860002100Payroll19864.730
16210562026-06-2860002200Payroll9885.930
16310562026-06-2860003000Payroll13419.990
16410562026-06-2860003100Payroll6874.020
16510562026-06-281000NULLPayroll0108002.54
16610572026-06-301100NULLSales invoices293910.520
16710572026-06-304000NULLSales invoices0293910.52
16810582026-06-301000NULLCustomer receipts416664.20
16910582026-06-301100NULLCustomer receipts0416664.2
cost_centers 8 rows, 4 columns
cost_center_idnamedepartmentmanager
1000ManagementAdministrationLaura Bennett
1100Finance and HRAdministrationDaniel Price
1200ITAdministrationRyan Cooper
2000ManufacturingOperationsMark Ellis
2100AssemblyOperationsSofia Ramirez
2200Warehouse and shippingOperationsKevin Shaw
3000SalesSalesJames Turner
3100MarketingSalesEmily Ward
accounts 13 rows, 3 columns
account_idnametype
1000Bankasset
1100Accounts receivableasset
2000Accounts payableliability
3000Equityequity
4000Sales revenuerevenue
5000Materialsexpense
6000Salaries and wagesexpense
6100Rent and occupancyexpense
6200Energyexpense
6300Travelexpense
6400Advertisingexpense
6500IT and softwareexpense
6600Repairs and maintenanceexpense

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.

Next: case 5, cash flow

Will the cash last until year end? You turn the cash plan into a forecast with a running bank balance.

Go to case 5

Back to the course overview.

Write to us

Choose language

Select your preferred language