Introduction to SQL Case Statements

preview_player
Показать описание
The SQL Case Statement is likely the most useful function to know when working with accounting data as it is used to create custom categories.

The syntax may look a little scary but once you work through a few examples it will become second nature. The purpose of a case statement is to create a conditional column based user defined conditions. Think of Grouping accounts by a selection of Entities, Cost Center or GL Accounts.

We start with the keyword CASE and end with END AS 'Column Name', the categories are defined in the middle by WHEN [condition] THEN 'Category Name' with leftovers going to ELSE 'Category Name'.

💡 Example - I want to categorise entities in to their regions in a new columns called region. 💡

CASE

WHEN ENTITY IN (100,101,102) THEN 'EMEA'

WHEN ENTITY IN (200,201,202) THEN 'APAC'

WHEN ENTITY IN (300,301,302) THEN 'US'

ELSE 'LATAM'

END AS 'Region'



In the below video I show a real life example in Google Big Query.

#DataAnalysis

#BusinessAnalytics

#FinanceAnalytics

#SQLTips

#SQL
Рекомендации по теме