Skip to content

Commit

Permalink
substitute create temp table for CREATE OR REPLACE temp table
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Timoteo committed Sep 19, 2023
1 parent 53ffb25 commit 0fead11
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 66 deletions.
4 changes: 2 additions & 2 deletions sql/query/invoke_backfill_customer_lifetime_value_label.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DECLARE min_date DATE;
SET max_date = (SELECT DATE_SUB(MAX(event_date), INTERVAL {{interval_max_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);
SET min_date = (SELECT DATE_ADD(MIN(event_date), INTERVAL {{interval_min_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);

CREATE TEMP TABLE dates_interval as (
CREATE OR REPLACE TEMP TABLE dates_interval as (
SELECT DISTINCT
event_date as input_date,
DATE_ADD(event_date, INTERVAL {{interval_end_date}} DAY) as end_date
Expand All @@ -28,7 +28,7 @@ CREATE TEMP TABLE dates_interval as (


## All users in the platform
CREATE TEMP TABLE events_users_days as (
CREATE OR REPLACE TEMP TABLE events_users_days as (
SELECT DISTINCT
Users.user_pseudo_id
FROM `{{mds_project_id}}.{{mds_dataset}}.event` Users
Expand Down
6 changes: 3 additions & 3 deletions sql/query/invoke_backfill_purchase_propensity_label.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DECLARE min_date DATE;
SET max_date = (SELECT DATE_SUB(MAX(event_date), INTERVAL {{interval_max_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);
SET min_date = (SELECT DATE_ADD(MIN(event_date), INTERVAL {{interval_min_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);

CREATE TEMP TABLE dates_interval as (
CREATE OR REPLACE TEMP TABLE dates_interval as (
SELECT DISTINCT
event_date as input_date,
DATE_ADD(event_date, INTERVAL {{interval_end_date}} DAY) as end_date
Expand All @@ -28,7 +28,7 @@ CREATE TEMP TABLE dates_interval as (
);

## All users in the platform
CREATE TEMP TABLE all_users_possible_purchases as (
CREATE OR REPLACE TEMP TABLE all_users_possible_purchases as (
SELECT DISTINCT
Users.user_pseudo_id,
DI.event_date as event_date,
Expand Down Expand Up @@ -56,7 +56,7 @@ CREATE TEMP TABLE all_users_possible_purchases as (
);

## Future User metrics: 1-7-day future purchases per user, 1-15-day future purchases per user, 1-30-day future purchases per user, 1–90-day future purchases per user
CREATE TEMP TABLE future_purchases_per_user AS (
CREATE OR REPLACE TEMP TABLE future_purchases_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand Down
8 changes: 4 additions & 4 deletions sql/query/invoke_backfill_user_dimensions.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DECLARE min_date DATE;
SET max_date = (SELECT DATE_SUB(MAX(event_date), INTERVAL {{interval_max_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);
SET min_date = (SELECT DATE_ADD(MIN(event_date), INTERVAL {{interval_min_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);

CREATE TEMP TABLE dates_interval as (
CREATE OR REPLACE TEMP TABLE dates_interval as (
SELECT DISTINCT
event_date as input_date,
DATE_SUB(event_date, INTERVAL {{interval_end_date}} DAY) as end_date
Expand All @@ -26,7 +26,7 @@ CREATE TEMP TABLE dates_interval as (
ORDER BY input_date DESC)
;

CREATE TEMP TABLE user_dimensions_event_scoped as (
CREATE OR REPLACE TEMP TABLE user_dimensions_event_scoped as (
SELECT DISTINCT
event_date as feature_date,
user_pseudo_id,
Expand Down Expand Up @@ -62,7 +62,7 @@ CREATE TEMP TABLE user_dimensions_event_scoped as (
)
;

CREATE TEMP TABLE user_dimensions_session_scoped as (
CREATE OR REPLACE TEMP TABLE user_dimensions_session_scoped as (
SELECT DISTINCT
event_date as feature_date,
user_pseudo_id,
Expand Down Expand Up @@ -106,7 +106,7 @@ CREATE TEMP TABLE user_dimensions_session_scoped as (
;

-- All users in the platform
CREATE TEMP TABLE events_users as (
CREATE OR REPLACE TEMP TABLE events_users as (
SELECT DISTINCT
Users.user_pseudo_id,
Users.user_id,
Expand Down
6 changes: 3 additions & 3 deletions sql/query/invoke_backfill_user_lifetime_dimensions.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DECLARE min_date DATE;
SET max_date = (SELECT DATE_SUB(MAX(event_date), INTERVAL {{interval_max_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);
SET min_date = (SELECT DATE_ADD(MIN(event_date), INTERVAL {{interval_min_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);

CREATE TEMP TABLE dates_interval as (
CREATE OR REPLACE TEMP TABLE dates_interval as (
SELECT DISTINCT
event_date as input_date,
DATE_SUB(event_date, INTERVAL {{interval_end_date}} DAY) as end_date
Expand All @@ -26,7 +26,7 @@ WHERE event_date BETWEEN min_date AND max_date
ORDER BY input_date DESC
);

CREATE TEMP TABLE user_dimensions_event_session_scoped as (
CREATE OR REPLACE TEMP TABLE user_dimensions_event_session_scoped as (
SELECT DISTINCT
event_date as feature_date,
user_pseudo_id,
Expand Down Expand Up @@ -74,7 +74,7 @@ AND D.device_os IS NOT NULL
);

-- All users in the platform
CREATE TEMP TABLE events_users as (
CREATE OR REPLACE TEMP TABLE events_users as (
SELECT DISTINCT
Users.user_pseudo_id,
Users.user_id,
Expand Down
18 changes: 9 additions & 9 deletions sql/query/invoke_backfill_user_lookback_metrics.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
DECLARE min_date DATE;
SET min_date = (SELECT DATE_ADD(MIN(event_date), INTERVAL {{interval_min_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);

CREATE TEMP TABLE dates_interval as (
CREATE OR REPLACE TEMP TABLE dates_interval as (
SELECT DISTINCT
event_date as input_date,
DATE_SUB(event_date, INTERVAL {{interval_end_date}} DAY) as end_date
Expand All @@ -25,7 +25,7 @@ CREATE TEMP TABLE dates_interval as (
);

##All users metrics: 7-day active users, 8–14-day active users
CREATE TEMP TABLE rolling_active_users AS (
CREATE OR REPLACE TEMP TABLE rolling_active_users AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -39,7 +39,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day purchase per user, 7-day purchases per user, 8-14-day purchases per user
CREATE TEMP TABLE rolling_purchases_past_days AS (
CREATE OR REPLACE TEMP TABLE rolling_purchases_past_days AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -56,7 +56,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day visits per user, 7-day visits per user, 8-14-day visits per user
CREATE TEMP TABLE rolling_visits_past_days AS (
CREATE OR REPLACE TEMP TABLE rolling_visits_past_days AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -70,7 +70,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day view_item per user, 7-day view_item per user, 8-14-day view_item per user
CREATE TEMP TABLE rolling_view_item_past_days AS (
CREATE OR REPLACE TEMP TABLE rolling_view_item_past_days AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -85,7 +85,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day add_to_cart per user, 7-day add_to_cart per user, 8-14-day add_to_cart per user
CREATE TEMP TABLE rolling_add_to_cart_past_days AS (
CREATE OR REPLACE TEMP TABLE rolling_add_to_cart_past_days AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -100,7 +100,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day checkout per user, 7-day checkout per user, 8-14-day checkout per user
CREATE TEMP TABLE rolling_checkout_past_days AS (
CREATE OR REPLACE TEMP TABLE rolling_checkout_past_days AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -115,7 +115,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past revenue metrics
CREATE TEMP TABLE rolling_revenue_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_revenue_per_user AS (
SELECT DISTINCT
user_pseudo_id,
input_date as feature_date,
Expand Down Expand Up @@ -144,7 +144,7 @@ GROUP BY user_pseudo_id, feature_date
);

-- All users in the platform
CREATE TEMP TABLE events_users as (
CREATE OR REPLACE TEMP TABLE events_users as (
SELECT DISTINCT
Users.user_pseudo_id,
DI.input_date as feature_date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DECLARE min_date DATE;
SET max_date = (SELECT DATE_SUB(MAX(event_date), INTERVAL {{interval_max_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);
SET min_date = (SELECT DATE_ADD(MIN(event_date), INTERVAL {{interval_min_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);

CREATE TEMP TABLE dates_interval as (
CREATE OR REPLACE TEMP TABLE dates_interval as (
SELECT DISTINCT
event_date as input_date,
DATE_SUB(event_date, INTERVAL {{interval_end_date}} DAY) as end_date
Expand All @@ -28,7 +28,7 @@ WHERE event_date BETWEEN min_date AND max_date

# Run these windows aggregations every day. For each date in training and inference date ranges.
##All users metrics: 7-day active users, 8–30-day active users, 31-90-day active users
CREATE TEMP TABLE rolling_active_users AS (
CREATE OR REPLACE TEMP TABLE rolling_active_users AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -46,7 +46,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day purchase per user, 2-7-day purchases per user, 8-14-day purchases per user, 15-30-day purchases per user, 31–90-day purchases per user
CREATE TEMP TABLE rolling_purchases_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_purchases_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -67,7 +67,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day visits per user, 2-7-day visits per user, 7-14-day visits per user, 15-30-day visits per user, 31–90-day visits per user
CREATE TEMP TABLE rolling_visits_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_visits_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -85,7 +85,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day view_item per user, 2-7-day view_item per user, 7-14-day view_item per user, 15-30-day view_item per user, 31–90-day view_item per user
CREATE TEMP TABLE rolling_view_item_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_view_item_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -104,7 +104,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day add_to_cart per user, 2-7-day add_to_cart per user, 7-14-day add_to_cart per user, 15-30-day add_to_cart per user, 31–90-day add_to_cart per user, 1-day checkout per user, 2-7-day checkout per user, 7-14-day checkout per user, 15-30-day checkout per user, 31–90-day checkout per user
CREATE TEMP TABLE rolling_add_to_cart_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_add_to_cart_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -123,7 +123,7 @@ GROUP BY user_pseudo_id, feature_date
);

## Past User metrics: 1-day visits per user, 2-7-day visits per user, 7-14-day visits per user, 15-30-day visits per user, 31–90-day visits per user, 1-day view_item per user, 2-7-day view_item per user, 7-14-day view_item per user, 15-30-day view_item per user, 31–90-day view_item per user, 1-day add_to_cart per user, 2-7-day add_to_cart per user, 7-14-day add_to_cart per user, 15-30-day add_to_cart per user, 31–90-day add_to_cart per user, 1-day checkout per user, 2-7-day checkout per user, 7-14-day checkout per user, 15-30-day checkout per user, 31–90-day checkout per user
CREATE TEMP TABLE rolling_checkout_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_checkout_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -141,7 +141,7 @@ AND ga_session_id IS NOT NULL
GROUP BY user_pseudo_id, feature_date
);

CREATE TEMP TABLE rolling_revenue_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_revenue_per_user AS (
SELECT DISTINCT
user_pseudo_id,
input_date as feature_date,
Expand Down Expand Up @@ -175,7 +175,7 @@ GROUP BY user_pseudo_id, feature_date
);

## All users in the platform
CREATE TEMP TABLE events_users as (
CREATE OR REPLACE TEMP TABLE events_users as (
SELECT DISTINCT
Users.user_pseudo_id,
DI.input_date as feature_date
Expand Down
16 changes: 8 additions & 8 deletions sql/query/invoke_backfill_user_rolling_window_metrics.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DECLARE min_date DATE;
SET max_date = (SELECT DATE_SUB(MAX(event_date), INTERVAL {{interval_max_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);
SET min_date = (SELECT DATE_ADD(MIN(event_date), INTERVAL {{interval_min_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);

CREATE TEMP TABLE dates_interval as (
CREATE OR REPLACE TEMP TABLE dates_interval as (
SELECT DISTINCT
event_date as input_date,
DATE_SUB(event_date, INTERVAL {{interval_end_date}} DAY) as end_date
Expand All @@ -28,7 +28,7 @@ CREATE TEMP TABLE dates_interval as (

# Run these windows aggregations every day. For each date in training and inference date ranges.
##All users metrics: 7-day active users, 8–30-day active users, 31-90-day active users
CREATE TEMP TABLE rolling_active_users AS (
CREATE OR REPLACE TEMP TABLE rolling_active_users AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -49,7 +49,7 @@ CREATE TEMP TABLE rolling_active_users AS (
);

## Past User metrics: 1-day purchase per user, 2-7-day purchases per user, 8-14-day purchases per user, 15-30-day purchases per user, 31–90-day purchases per user
CREATE TEMP TABLE rolling_purchases_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_purchases_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -73,7 +73,7 @@ CREATE TEMP TABLE rolling_purchases_per_user AS (
);

## Past User metrics: 1-day visits per user, 2-7-day visits per user, 7-14-day visits per user, 15-30-day visits per user, 31–90-day visits per user
CREATE TEMP TABLE rolling_visits_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_visits_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -94,7 +94,7 @@ CREATE TEMP TABLE rolling_visits_per_user AS (
);

## Past User metrics: 1-day view_item per user, 2-7-day view_item per user, 7-14-day view_item per user, 15-30-day view_item per user, 31–90-day view_item per user
CREATE TEMP TABLE rolling_view_item_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_view_item_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -116,7 +116,7 @@ CREATE TEMP TABLE rolling_view_item_per_user AS (
);

## Past User metrics: 1-day add_to_cart per user, 2-7-day add_to_cart per user, 7-14-day add_to_cart per user, 15-30-day add_to_cart per user, 31–90-day add_to_cart per user, 1-day checkout per user, 2-7-day checkout per user, 7-14-day checkout per user, 15-30-day checkout per user, 31–90-day checkout per user
CREATE TEMP TABLE rolling_add_to_cart_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_add_to_cart_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -138,7 +138,7 @@ CREATE TEMP TABLE rolling_add_to_cart_per_user AS (
);

## Past User metrics: 1-day visits per user, 2-7-day visits per user, 7-14-day visits per user, 15-30-day visits per user, 31–90-day visits per user, 1-day view_item per user, 2-7-day view_item per user, 7-14-day view_item per user, 15-30-day view_item per user, 31–90-day view_item per user, 1-day add_to_cart per user, 2-7-day add_to_cart per user, 7-14-day add_to_cart per user, 15-30-day add_to_cart per user, 31–90-day add_to_cart per user, 1-day checkout per user, 2-7-day checkout per user, 7-14-day checkout per user, 15-30-day checkout per user, 31–90-day checkout per user
CREATE TEMP TABLE rolling_checkout_per_user AS (
CREATE OR REPLACE TEMP TABLE rolling_checkout_per_user AS (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -160,7 +160,7 @@ CREATE TEMP TABLE rolling_checkout_per_user AS (
);

## All users in the platform
CREATE TEMP TABLE events_users as (
CREATE OR REPLACE TEMP TABLE events_users as (
SELECT DISTINCT
Users.user_pseudo_id,
DI.input_date as feature_date
Expand Down
10 changes: 5 additions & 5 deletions sql/query/invoke_backfill_user_scoped_lifetime_metrics.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DECLARE min_date DATE;
SET max_date = (SELECT DATE_SUB(MAX(event_date), INTERVAL {{interval_max_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);
SET min_date = (SELECT DATE_ADD(MIN(event_date), INTERVAL {{interval_min_date}} DAY) FROM `{{mds_project_id}}.{{mds_dataset}}.event`);

CREATE TEMP TABLE dates_interval as (
CREATE OR REPLACE TEMP TABLE dates_interval as (
SELECT DISTINCT
event_date as input_date,
DATE_SUB(event_date, INTERVAL {{interval_end_date}} DAY) as end_date
Expand All @@ -28,7 +28,7 @@ CREATE TEMP TABLE dates_interval as (

# Run these once each day.
## Active users, Average daily purchasers, Average engagement time, Average engagement time per session, DAU / MAU, DAU / WAU, First time purchasers, First-time purchaser conversion, First-time purchasers per new user, Max daily purchasers, Min daily purchasers, New users, Returning users, Total purchasers, Total users, User conversion rate, User engagement, WAU / MAU
CREATE TEMP TABLE engagement as (
CREATE OR REPLACE TEMP TABLE engagement as (
SELECT
user_pseudo_id,
input_date as feature_date,
Expand All @@ -47,7 +47,7 @@ CREATE TEMP TABLE engagement as (
GROUP BY user_pseudo_id, feature_date, session_id
);

CREATE TEMP TABLE revenue_users as (
CREATE OR REPLACE TEMP TABLE revenue_users as (
SELECT
input_date as feature_date,
SUM(ecommerce.purchase_revenue_in_usd) as sum_revenue_per_day,
Expand All @@ -62,7 +62,7 @@ CREATE TEMP TABLE revenue_users as (
GROUP BY feature_date
);

CREATE TEMP TABLE first_purchasers as (
CREATE OR REPLACE TEMP TABLE first_purchasers as (
SELECT
input_date as feature_date,
COUNT(first_time_purchasers) as first_time_purchasers
Expand All @@ -89,7 +89,7 @@ CREATE TEMP TABLE first_purchasers as (
GROUP BY feature_date
);

CREATE TEMP TABLE new_users_ as (
CREATE OR REPLACE TEMP TABLE new_users_ as (
SELECT
input_date as feature_date,
COUNT(distinct case when event_name="first_visit" then user_pseudo_id end) as new_users
Expand Down
Loading

0 comments on commit 0fead11

Please sign in to comment.