Week 5

Combining Data and Relationships

Instructors
Maclean Gaulin

Industry Outliers

TickerYearIndustryNIEPS
BOOT2024Consumer Discr.180.95.93
BRSL2024Consumer Discr.348.01.73
BRIA2024Consumer Discr.2.80.11
ETD2024Consumer Discr.63.82.50
EDAP2024Health Care(19.6)(0.53)
CNTB2024Health Care(15.6)(0.28)
SPRO2024Health Care(68.5)(1.27)
PCLOF2024Health Care(11.3)(0.07)
  • (BRILLIA INC)
  • (PHARMACIELO LTD)
  • How to compare firms to the industry “norm”?

Why combine data?

  • Myriad data sources
  • External data (marketing data, economic data, etc.)
  • Various internal systems (invoices, production, etc.)
  • Aggregation
  • Aggregation can require combining data with itself
  • Analysis, cleaning, and verification
  • Comparing observations, interpolating, etc.

Relational Database

  • Database
  • Table 1: GL_Detail
  • Table 2: Chart_of_Accounts
journal_idline_idaccount_idamount
119003500
122002500
139500350
144002350
account_idaccount_namesegment
1000CashSW
2002ReceivablesSW
4002InventorySW
8002Retained Earn.SW
9003RevenueSW
9500COGSSW

Relational database

  • Relations often through “keys” that link tables
  • Primary key is used to define an observation (row) in a table
  • Foreign key is included in a table to link to the primary key of another table (link the rows)
  • E.g. account_id is a foreign key in GL_Detail, and the primary key in Chart_of_Accounts

Base & General Ledger

Base & General Ledger

Relationship Types

  • Relationships can be singular or multiple
  • 1:1 – one row in table 1 matches one row in table 2
  • 1:m – one row in T1 matches multiple rows in T2
  • E.g. 1 employee matches with multiple salary payments
  • m:1 – multiple rows in T1 match with one row in T2
  • E.g. multiple employees will match with 1 manager
  • m:m – multiple rows in T1 match multiple rows in T2
  • E.g. employees could be in multiple committees, and the committees have multiple employees in them

Many to Many relationships

employee_idname
0Alice
1Bob
2Candice
employee_idcommittee_id
00
01
10
12
21
22
committee_idname
0Xcelerate Growth Strategy
1Year-End Financial Review
2Zero-Based Budgeting
EmployeeCommittee
AliceXcelerate Growth Strategy
AliceYear-End Financial Review
BobXcelerate Growth Strategy
BobZero-Based Budgeting
CandiceYear-End Financial Review
CandiceZero-Based Budgeting

AICPA Standards

  • Base standard addressing users, business units, segments, and tax
  • General Ledger (chart of accounts, GL, trial balance)
  • Order to Cash subledger
  • Procure to Pay subledger
  • Inventory subledger
  • Fixed Asset subledger

Order to Cash subledger

Order to Cash subledger

SQL: Structured Query Language

  • SQL is the language used to interact with RDBs
  • SQL playground: sqlbolt.com
  • Do you need to know SQL?
  • You should understand conceptually the moving parts
  • You should understand what it can and can’t do
  • Which you can ask LLMs
  • Learn it if you need it, are interested, or taking the ISC section of the CPA

sqlbolt.com

Types of joins

  • Inner: just keep rows that match
  • Outer: keep all rows
  • Left/right: keep all rows from one table
  • Cross: every row in one table for every row in the other
  • Self: joining a table with itself (could be inner, outer, or left/right)
  • Union: concatenating datasets, e.g. adding rows together, not columns together

Inner join

  • Just keep rows that match (join keys are in both tables)
  • Most common join (JOIN keyword in SQL is inner)

Outer join

  • Keeps all rows from both tables
  • Rows that do not match other table are filled with missing (NULL)
  • Can create large datasets
NULLs
NULLs

Left/Right join

  • Keeps all rows in one table
  • Used to add columns from another table without altering original rows
  • Left and right join are the same with swapped table orders
  • A left join B = B right join A
NULLs

Cross Join

  • Creates all combinations of rows
  • Every row in T1 will be merged with every row in T2
  • Used to expand datasets
  • Example: going from yearly to monthly
  • Used to get all combinations of values
  • Example: match every buyer to every seller to find best trades

Range Merges

  • Join tables based on data from one being within a range based on columns from the other
  • E.g. Get last year of transactions (with fiscal year end)
  • SQL: b.date BETWEEN a.last_fye AND a.fye
  • Used for time-series, binning, finding overlaps, etc.
  • Considering overlaps is key to avoid duplication

Range Merges

  • Most accounting/financial data is time-based, range merges very common
  • Returns Dataset
datetickerreturns
1/3/2023AAPL-3.7%
1/4/2023AAPL1.0%
1/5/2023AAPL-1.1%
1/6/2023AAPL3.7%
1/9/2023AAPL0.4%
………
12/29/2023AAPL-0.5%
  • Financials Dataset
fyetickerAT
12/31/2023AAPL352,583
12/31/2024AAPL364,980

Multiple Merges

  • Connecting merges are a common use of multiple merges
  • Often built one merge at a time
  • Transaction Dataset
  • Customer Dataset
  • Financials Dataset
customer_idName
0Alice
1Bob
……
datecustomer_idamount
1/3/20230156.34
1/4/202312,000.41
1/5/2023175.05
………
fyetickerAT
12/31/2023AAPL352,583
12/31/2024AAPL364,980

Merging without keys

  • Keys work for perfect matches (equality)
  • What about when there is no key?
  • Data from external source
  • Data with noise or errors
  • Data with non-perfect overlap (e.g. spatial data)
  • Statistical / fuzzy matching used in these cases

Aggregations

  • Calculations across multiple rows
  • Counting, summing, averaging, regressions, etc.
  • Aggregations collapse multiple rows into one
  • Often combined with merges
  • E.g. total expenses by vendor within date range
  • Often done in “subquery” and merged

Aggregations

  • Adding yearly returns to financial dataset
  • Returns Dataset
  • Financials Dataset
  • sub-query
fyetickerAT
12/31/2023AAPL352,583
12/31/2024AAPL364,980
datetickerreturns
1/3/2023AAPL-3.7%
………
12/29/2023AAPL-0.5%
1/2/2024AAPL-3.5%
………
12/29/2024AAPL-0.7%
1/3/2023GOOGL-2.5%
………
fyeartickerave_retcuml_ret
2023AAPL0.1%49.0%
2024AAPL0.1%30.7%
2023GOOG0.2%58.8%

Levels of Aggregation

YearMonthDayStateCityAmount
202411UTSLC10
202412UTSLC11
202421UTSLC12
202422UTSLC13
202311UTSLC14
202312UTSLC15
202321UTSLC16
202322UTSLC17
202411UTProvo20
202412UTProvo21
202421UTProvo22
202422UTProvo23
202311UTProvo24
202312UTProvo25
202321UTProvo26
202322UTProvo27
202411CODenver30
202412CODenver31
202421CODenver32
202422CODenver33
202311CODenver34
202312CODenver35
202321CODenver36
202322CODenver37
202411COBoulder40
202412COBoulder41
202421COBoulder42
202422COBoulder43
202311COBoulder44
202312COBoulder45
202321COBoulder46
202322COBoulder47
  • The same data can be aggregated many different ways
  • How we aggregate changes what information we get
YearMonthDayStateCityAmount
202411UTSLC10
202412UTSLC11
202421UTSLC12
202422UTSLC13
202311UTSLC14
202312UTSLC15
202321UTSLC16
202322UTSLC17
202411UTProvo20
202412UTProvo21
202421UTProvo22
202422UTProvo23
202311UTProvo24
202312UTProvo25
202321UTProvo26
202322UTProvo27
202411CODenver30
202412CODenver31
202421CODenver32
202422CODenver33
202311CODenver34
202312CODenver35
202321CODenver36
202322CODenver37
202411COBoulder40
202412COBoulder41
202421COBoulder42
202422COBoulder43
202311COBoulder44
202312COBoulder45
202321COBoulder46
202322COBoulder47
  • Group by Year
  • Average across location, within year
YearsAverage Amount
202330.5
202426.5
YearMonthDayStateCityAmount
202411UTSLC10
202412UTSLC11
202421UTSLC12
202422UTSLC13
202311UTSLC14
202312UTSLC15
202321UTSLC16
202322UTSLC17
202411UTProvo20
202412UTProvo21
202421UTProvo22
202422UTProvo23
202311UTProvo24
202312UTProvo25
202321UTProvo26
202322UTProvo27
202411CODenver30
202412CODenver31
202421CODenver32
202422CODenver33
202311CODenver34
202312CODenver35
202321CODenver36
202322CODenver37
202411COBoulder40
202412COBoulder41
202421COBoulder42
202422COBoulder43
202311COBoulder44
202312COBoulder45
202321COBoulder46
202322COBoulder47
  • Group by Year and Month
  • Average across location, within year and month
Year/MonthAverage Amount
2023 – 0129.5
2023 – 0231.5
2024 – 0125.5
2024 – 0227.5
YearMonthDayStateCityAmount
202411UTSLC10
202412UTSLC11
202421UTSLC12
202422UTSLC13
202311UTSLC14
202312UTSLC15
202321UTSLC16
202322UTSLC17
202411UTProvo20
202412UTProvo21
202421UTProvo22
202422UTProvo23
202311UTProvo24
202312UTProvo25
202321UTProvo26
202322UTProvo27
202411CODenver30
202412CODenver31
202421CODenver32
202422CODenver33
202311CODenver34
202312CODenver35
202321CODenver36
202322CODenver37
202411COBoulder40
202412COBoulder41
202421COBoulder42
202422COBoulder43
202311COBoulder44
202312COBoulder45
202321COBoulder46
202322COBoulder47
  • Group by Year and State
  • Average across City and month/day, within year and state
Year/StateAverage Amount
2023 UT20.5
2023 CO40.5
2024 UT16.5
2024 CO36.5
YearMonthDayStateCityAmount
202411UTSLC10
202412UTSLC11
202421UTSLC12
202422UTSLC13
202311UTSLC14
202312UTSLC15
202321UTSLC16
202322UTSLC17
202411UTProvo20
202412UTProvo21
202421UTProvo22
202422UTProvo23
202311UTProvo24
202312UTProvo25
202321UTProvo26
202322UTProvo27
202411CODenver30
202412CODenver31
202421CODenver32
202422CODenver33
202311CODenver34
202312CODenver35
202321CODenver36
202322CODenver37
202411COBoulder40
202412COBoulder41
202421COBoulder42
202422COBoulder43
202311COBoulder44
202312COBoulder45
202321COBoulder46
202322COBoulder47
  • Group by City
  • Average across City, within year/month/day
Year/StateAverage Amount
SLC13.5
Provo23.5
Denver33.5
Boulder43.5

Self Joins – Time series

  • Lead/Lag variables can also be merges
  • JOIN ON ticker
  • AND fyear = fyear + 1
fyeartickerave_ret
2023AAPL0.2%
2024AAPL0.1%
2023GOOG0.2%
fyeartickerave_retave_ret_last_year
2023AAPL0.2%–
2024AAPL0.1%0.2%
2023GOOG0.2%–
fyearfyear+1tickerave_ret
20232024AAPL0.2%
20242025AAPL0.1%
20232024GOOG0.2%
Combining Data and Relationships