# Slowly Changing Dimensions (SCD)

## Introduction

Slowly changing dimensions (SCD) are used to capture changing data within the dimension over time. How could we track the changes in the example below?

<figure><img src="https://3167488510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN5Oxfr1ogoEGQMX2dzNM%2Fuploads%2FH20u2yobwXaGrDIINwox%2Fimage.png?alt=media&#x26;token=0e4c3384-f49e-4467-9da1-257d82e1b1ed" alt=""><figcaption></figcaption></figure>

## Types of SCD

### Type 1 - Overwrite

* Replace all existing data
* Lose historical data
* Often used for data corrections

<figure><img src="https://3167488510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN5Oxfr1ogoEGQMX2dzNM%2Fuploads%2Fr5SMjBsLpKpTQjkfGBxv%2Fimage.png?alt=media&#x26;token=45036e19-2c7d-4587-b55b-088392a8a351" alt=""><figcaption></figcaption></figure>

### Type 2 - Add historical columns

To analyse historical data, you will need to understand when the customer was from Corporate and Retail.

* Creates additional records on each changing record event
* History of dimension changes are kept
* Expensive database operation
* Adds a surrogate key

<figure><img src="https://3167488510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN5Oxfr1ogoEGQMX2dzNM%2Fuploads%2F7j91A70qsyf3xfvo4rzi%2Fimage.png?alt=media&#x26;token=94222805-7fa2-4d5c-9f8e-abc0f6da4628" alt=""><figcaption></figcaption></figure>

#### Surrogate key <a href="#surrogate-key" id="surrogate-key"></a>

* Sequentially generated unique number
* Eliminate source system dependency
* Increase performance with using suitable data type on joins
