Skip to content

Instantly share code, notes, and snippets.

@Mr--John-Doe
Mr--John-Doe / original_query_with_variant_types.sql
Last active June 22, 2022 09:20
original query - variants
SELECT da.username
, da.value:date as date
, da.value:hit_count as hit_count
, ua_1.value:user_address as user_address
, ua_2.value:user_mobile as user_mobile
, ua_3_optin.user_email_optin as user_email_optim
, ua_3_optin.optin_date as user_email_optin_date
WITH
union_tab as (
select username, 'daily_activity' as label, value from web.daily_activity UNION ALL
select username, 'user_address' as label, value from web.user_attributes_1 UNION ALL
select username, 'user_address' as label, value from web.user_attributes_1 UNION ALL
select username, 'user_mobile' as label, value from web.user_attributes_2 UNION ALL
select username, 'user_email_optin' as label, value from web.user_attributes_3 UNION ALL
select username, 'user_mobile_optin' as label, value from web.user_attributes_4 UNION ALL
(... and so on ...)
)
WITH FULL_SET AS (
SELECT username, hit_count, NULL AS user_address, NULL AS user_mobile, NULL AS user_email_optin
FROM web.user_activity_summary
UNION ALL
SELECT username, NULL, user_address, NULL, NULL
FROM web.user_attributes_1
WHERE ua_1.attribute_name = 'MAIN_ADDRESS'
UNION ALL
SELECT username, NULL, NULL, user_mobile, NULL
FROM web.user_attributes_2
SELECT da.username
, da.date
, da.hit_count
, ua_1.value as user_address
, ua_2.value as user_mobile
, ua_3.value as user_email_optin
FROM web.daily_activity da
LEFT JOIN web.user_attributes_1 ua_1
ON ua_1.USERNAME = da.USERNAME
AND ua_1.attribute_name = 'MAIN_ADDRESS'
@Mr--John-Doe
Mr--John-Doe / snowflake_table_lineage.sql
Last active April 2, 2022 13:56
Gets a list of source/target tables for any query (only available if bundle 2021_10 is enabled)
WITH table_lineage_history AS (
SELECT r.value:"objectName"::varchar AS upstream_table_name
, r.value:"objectDomain"::varchar AS upstream_table_domain
, r.value:"columns" AS upstream_table_columns
, w.value:"objectName"::varchar AS downstream_table_name
, w.value:"objectDomain"::varchar AS downstream_table_domain
, w.value:"columns" AS downstream_table_columns
, t.query_start_time AS query_start_time
, t.QUERY_ID AS query_id
FROM SNOWFLAKE.ACCOUNT_USAGE.ACCESS_HISTORY t
WITH first_user_login AS (
SELECT user, MIN(login_ts) AS min_date
FROM logins
GROUP BY 1
)
SELECT user, platform as first_login_platform, ...
FROM logins
WHERE (user, login_ts) IN (SELECT user, min_date FROM first_user_login)
@Mr--John-Doe
Mr--John-Doe / query_tuning_1__slow_query.sql
Last active February 12, 2022 12:39
Tuning - slow query
SELECT DISTINCT username
, account_id
, FIRST_VALUE(session_id) OVER (PARTITION BY account_id ORDER BY login_ts) AS session_id
, FIRST_VALUE(ip_address) OVER (PARTITION BY account_id ORDER BY login_ts) AS ip_address
, FIRST_VALUE(country_id) OVER (PARTITION BY account_id ORDER BY login_ts) AS country_id
, FIRST_VALUE(website_entry_point) OVER (PARTITION BY account_id ORDER BY login_ts) AS website_entry_point
, FIRST_VALUE(payment_platform) OVER (PARTITION BY account_id ORDER BY login_ts) AS payment_platform
, FIRST_VALUE(platform_type) OVER (PARTITION BY account_id ORDER BY login_ts) AS platform_type
, FIRST_VALUE(session_duration_s) OVER (PARTITION BY account_id ORDER BY login_ts) AS session_duration_s
, FIRST_VALUE(number_sold_items) OVER (PARTITION BY account_id ORDER BY login_ts) AS number_sold_items
SELECT username
, account_id
, session_id
, ip_address
, country_id
, website_entry_point
, payment_platform
, platform_type
, session_duration_s
, number_sold_items
WITH base as (
SELECT *
, LAG(login_date) OVER (PARTITION BY username ORDER BY date) AS previous_login_date
FROM sales_site.login
)
SELECT *
FROM base
WHERE login_date between $FROM_DATE and $TO_DATE;
....
SET FROM_DATE = $RUN_DATE-7;
SET TO_DATE = $RUN_DATE;
/* We don't need much from before $FROM_DATE, just the max(login_date) */
CREATE TEMP TABLE temp_previous_login__pre_window AS
SELECT username AS username
, MAX(login_date) AS pre_window_login_date
FROM sales_site.login