CREATE OR REPLACE TABLE dv_stg.orders_20190806_000000
  (h_customer_hashkey TEXT (32) NOT NULL, r_timestamp TIMESTAMP_NTZ NOT NULL, r_source TEXT NOT NULL, customer_id TEXT NOT NULL, h_customer_role_playing_hashkey TEXT (32) NOT NULL, customer_role_playing_id TEXT NOT NULL, h_order_hashkey TEXT (32) NOT NULL, order_id TEXT NOT NULL, l_order_customer_hashkey TEXT (32) NOT NULL, ck_test_string TEXT NOT NULL, ck_test_timestamp TIMESTAMP_NTZ NOT NULL, l_order_customer_role_playing_hashkey TEXT (32) NOT NULL, hs_customer_hashdiff TEXT (32) NOT NULL, test_string TEXT, test_date DATE, test_timestamp_ntz TIMESTAMP_NTZ, test_integer NUMBER (38, 0), test_decimal NUMBER (18, 8), x_customer_id TEXT, grouping_key TEXT, test_geography GEOGRAPHY, test_array ARRAY, test_object OBJECT, test_variant VARIANT, test_timestamp_tz TIMESTAMP_TZ, test_timestamp_ltz TIMESTAMP_LTZ, test_time TIME, test_boolean BOOLEAN, test_real REAL, ls_order_customer_eff_hashdiff TEXT (32) NOT NULL, dummy_descriptive_field TEXT NOT NULL, ls_order_customer_role_playing_eff_hashdiff TEXT (32) NOT NULL) AS
  SELECT MD5(COALESCE(CAST(customer_id AS TEXT), 'dv_unknown')) AS h_customer_hashkey, CAST('2019-08-06T00:00:00.000000Z' AS TIMESTAMP) AS r_timestamp, 'test' AS r_source, COALESCE(customer_id, 'dv_unknown') AS customer_id, MD5(COALESCE(CAST(customer_role_playing_id AS TEXT), 'dv_unknown')) AS h_customer_role_playing_hashkey, COALESCE(customer_role_playing_id, 'dv_unknown') AS customer_role_playing_id, MD5(COALESCE(CAST(order_id AS TEXT), 'dv_unknown')) AS h_order_hashkey, COALESCE(order_id, 'dv_unknown') AS order_id, MD5(COALESCE(CAST(order_id AS TEXT), 'dv_unknown')||'|~~|'||COALESCE(CAST(customer_id AS TEXT), 'dv_unknown')||'|~~|'||COALESCE(CAST(ck_test_string AS TEXT), '')||'|~~|'||COALESCE(TO_CHAR(CAST(ck_test_timestamp AS TIMESTAMP_NTZ), 'yyyy-mm-dd hh24:mi:ss.ff9'), '')) AS l_order_customer_hashkey, ck_test_string, ck_test_timestamp, MD5(COALESCE(CAST(order_id AS TEXT), 'dv_unknown')||'|~~|'||COALESCE(CAST(customer_role_playing_id AS TEXT), 'dv_unknown')||'|~~|'||COALESCE(CAST(ck_test_string AS TEXT), '')||'|~~|'||COALESCE(TO_CHAR(CAST(ck_test_timestamp AS TIMESTAMP_NTZ), 'yyyy-mm-dd hh24:mi:ss.ff9'), '')) AS l_order_customer_role_playing_hashkey, MD5(REGEXP_REPLACE(COALESCE(CAST(customer_id AS TEXT), 'dv_unknown')||'|~~|'||COALESCE(CAST(test_string AS TEXT), '')||'|~~|'||COALESCE(TO_CHAR(CAST(test_date AS DATE), 'yyyy-mm-dd'), '')||'|~~|'||COALESCE(TO_CHAR(CAST(test_timestamp_ntz AS TIMESTAMP_NTZ), 'yyyy-mm-dd hh24:mi:ss.ff9'), '')||'|~~|'||COALESCE(CAST(CAST(test_integer AS NUMBER (38, 0)) AS TEXT), '')||'|~~|'||COALESCE(CAST(CAST(test_decimal AS NUMBER (18, 8)) AS TEXT), '')||'|~~|'||COALESCE(CAST(x_customer_id AS TEXT), '')||'|~~|'||COALESCE(CAST(grouping_key AS TEXT), '')||'|~~|'||COALESCE(ST_ASTEXT(TO_GEOGRAPHY(test_geography)), '')||'|~~|'||COALESCE(CAST(CAST(test_array AS ARRAY) AS TEXT), '')||'|~~|'||COALESCE(CAST(CAST(test_object AS OBJECT) AS TEXT), '')||'|~~|'||COALESCE(CAST(CAST(test_variant AS VARIANT) AS TEXT), '')||'|~~|'||COALESCE(TO_CHAR(CAST(test_timestamp_tz AS TIMESTAMP_TZ), 'yyyy-mm-dd hh24:mi:ss.ff9 tzhtzm'), '')||'|~~|'||COALESCE(TO_CHAR(CAST(test_timestamp_ltz AS TIMESTAMP_LTZ), 'yyyy-mm-dd hh24:mi:ss.ff9 tzhtzm'), '')||'|~~|'||COALESCE(TO_CHAR(CAST(test_time AS TIME), 'hh24:mi:ss.ff9'), '')||'|~~|'||COALESCE(CAST(CAST(test_boolean AS BOOLEAN) AS TEXT), '')||'|~~|'||COALESCE(CAST(CAST(test_real AS REAL) AS TEXT), ''), '(\\|~~\\|)+$', '')) AS hs_customer_hashdiff, test_string, test_date, test_timestamp_ntz, test_integer, test_decimal, x_customer_id, grouping_key, test_geography, test_array, test_object, test_variant, test_timestamp_tz, test_timestamp_ltz, test_time, test_boolean, test_real, MD5(REGEXP_REPLACE(COALESCE(CAST(order_id AS TEXT), 'dv_unknown')||'|~~|'||COALESCE(CAST(customer_id AS TEXT), 'dv_unknown')||'|~~|'||COALESCE(CAST(ck_test_string AS TEXT), '')||'|~~|'||COALESCE(TO_CHAR(CAST(ck_test_timestamp AS TIMESTAMP_NTZ), 'yyyy-mm-dd hh24:mi:ss.ff9'), '')||'|~~|'||COALESCE(CAST(dummy_descriptive_field AS TEXT), ''), '(\\|~~\\|)+$', '')) AS ls_order_customer_eff_hashdiff, dummy_descriptive_field, MD5(REGEXP_REPLACE(COALESCE(CAST(order_id AS TEXT), 'dv_unknown')||'|~~|'||COALESCE(CAST(customer_role_playing_id AS TEXT), 'dv_unknown')||'|~~|'||COALESCE(CAST(ck_test_string AS TEXT), '')||'|~~|'||COALESCE(TO_CHAR(CAST(ck_test_timestamp AS TIMESTAMP_NTZ), 'yyyy-mm-dd hh24:mi:ss.ff9'), '')||'|~~|'||COALESCE(CAST(dummy_descriptive_field AS TEXT), ''), '(\\|~~\\|)+$', '')) AS ls_order_customer_role_playing_eff_hashdiff
  FROM dv_extract.extract_orders;
