WITH staging AS (
-- Generated by dbtvault.
WITH source_data AS (
SELECT
"CustomerKey",
"GeographyKey",
"CustomerLabel",
"Title",
"FirstName",
"MiddleName",
"LastName",
"NameStyle",
"BirthDate",
"MaritalStatus",
"Suffix",
"Gender",
"EmailAddress",
"YearlyIncome",
"TotalChildren",
"NumberChildrenAtHome",
"Education",
"Occupation",
"HouseOwnerFlag",
"NumberCarsOwned",
"AddressLine1",
"AddressLine2",
"Phone",
"DateFirstPurchase",
"CustomerType",
"CompanyName",
"ETLLoadID",
"LoadDate",
"UpdateDate"
FROM "dev"."dbo"."RAW__Contoso__dbo__Customer"
),
derived_columns AS (
SELECT
"CustomerKey",
"GeographyKey",
"CustomerLabel",
"Title",
"FirstName",
"MiddleName",
"LastName",
"NameStyle",
"BirthDate",
"MaritalStatus",
"Suffix",
"Gender",
"EmailAddress",
"YearlyIncome",
"TotalChildren",
"NumberChildrenAtHome",
"Education",
"Occupation",
"HouseOwnerFlag",
"NumberCarsOwned",
"AddressLine1",
"AddressLine2",
"Phone",
"DateFirstPurchase",
"CustomerType",
"CompanyName",
"ETLLoadID",
"LoadDate",
"UpdateDate",
'Contoso.dbo.Customer' AS "RECORD_SOURCE",
"CustomerKey" AS "Customer__Id"
FROM source_data
),
hashed_columns AS (
SELECT
"CustomerKey",
"GeographyKey",
"CustomerLabel",
"Title",
"FirstName",
"MiddleName",
"LastName",
"NameStyle",
"BirthDate",
"MaritalStatus",
"Suffix",
"Gender",
"EmailAddress",
"YearlyIncome",
"TotalChildren",
"NumberChildrenAtHome",
"Education",
"Occupation",
"HouseOwnerFlag",
"NumberCarsOwned",
"AddressLine1",
"AddressLine2",
"Phone",
"DateFirstPurchase",
"CustomerType",
"CompanyName",
"ETLLoadID",
"LoadDate",
"UpdateDate",
"RECORD_SOURCE",
"Customer__Id",
CAST(HASHBYTES('MD5', NULLIF(UPPER(TRIM(CAST("CustomerKey" AS VARCHAR(max)))), '')) AS BINARY(16)) AS "Customer__Hash_Key",
CAST(HASHBYTES('MD5', (CONCAT_WS('||',
ISNULL(NULLIF(UPPER(TRIM(CAST("CustomerKey" AS VARCHAR(max)))), ''), '^^'),
ISNULL(NULLIF(UPPER(TRIM(CAST("FirstName" AS VARCHAR(max)))), ''), '^^'),
ISNULL(NULLIF(UPPER(TRIM(CAST("Title" AS VARCHAR(max)))), ''), '^^')
))) AS BINARY(16)) AS "Customer__Hash_Diff"
FROM derived_columns
),
columns_to_select AS (
SELECT
"CustomerKey",
"GeographyKey",
"CustomerLabel",
"Title",
"FirstName",
"MiddleName",
"LastName",
"NameStyle",
"BirthDate",
"MaritalStatus",
"Suffix",
"Gender",
"EmailAddress",
"YearlyIncome",
"TotalChildren",
"NumberChildrenAtHome",
"Education",
"Occupation",
"HouseOwnerFlag",
"NumberCarsOwned",
"AddressLine1",
"AddressLine2",
"Phone",
"DateFirstPurchase",
"CustomerType",
"CompanyName",
"ETLLoadID",
"LoadDate",
"UpdateDate",
"RECORD_SOURCE",
"Customer__Id",
"Customer__Hash_Key",
"Customer__Hash_Diff"
FROM hashed_columns
)
SELECT * FROM columns_to_select
)
SELECT *
FROM staging