From 5c52310f72480833e2ba4ddf93d93e0f12fbf058 Mon Sep 17 00:00:00 2001 From: Chris Salvato Date: Mon, 7 Mar 2016 17:32:44 -0500 Subject: [PATCH] add external files for queries to keep code clean --- adwords_campaigns_query.sql | 7 ++++ influencer_metrics_query.sql | 4 +++ transactions_query.sql | 64 ++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 adwords_campaigns_query.sql create mode 100644 influencer_metrics_query.sql create mode 100644 transactions_query.sql diff --git a/adwords_campaigns_query.sql b/adwords_campaigns_query.sql new file mode 100644 index 0000000..ac507b8 --- /dev/null +++ b/adwords_campaigns_query.sql @@ -0,0 +1,7 @@ +"select + * +from + stage.gs_adwords_campaigns awc +where + awc.date between '@{start_date}' and '@{end_date}' +order by awc.date desc" \ No newline at end of file diff --git a/influencer_metrics_query.sql b/influencer_metrics_query.sql new file mode 100644 index 0000000..31893e7 --- /dev/null +++ b/influencer_metrics_query.sql @@ -0,0 +1,4 @@ +"select + * +from + fact_influencer_metrics fim" \ No newline at end of file diff --git a/transactions_query.sql b/transactions_query.sql new file mode 100644 index 0000000..f4f3d9a --- /dev/null +++ b/transactions_query.sql @@ -0,0 +1,64 @@ +"select + t.created_at as transaction_date + , du.name as user_name + , t.discount_amount + , ti.credit_used + , t.sales_tax_amount + , (CASE t.refunded + WHEN 't' THEN + ti.price + ELSE + 0 + END) as refund_amount + , (CASE + WHEN d.type = 'GiftCard' + THEN d.amount + ELSE + ti.price - t.sales_tax_amount + t.discount_amount + END) as retail_price + , (CASE t.refunded + WHEN 't' THEN + 0 + ELSE + ti.price - ti.credit_used + END) as money_in_the_bank_paid_to_us + , (CASE + WHEN d.type = 'GiftCard' + THEN 'Gift Card' + WHEN ti.recurring = 't' + THEN 'Recurring Food Purchase' + ELSE + 'One-Time Meal Purchase' + END + ) as purchase_type + --, ti.name as meal_plan + --, ti.recurring + -- , l.name + -- , l.city + -- , l.state + -- , l.store_front_id + -- , l.home_delivery + , mp.* + from + transactions t + inner join + transaction_items ti on t.id = ti.transaction_id + inner join + dim_users du on t.user_id = du.user_id + inner join + ( + select distinct on (gsm.user_id) + gsm.* + from + stage.gs_mixpanel gsm + order by gsm.user_id, mixpanel_event_timestamp + ) mp on du.user_id = mp.user_id + --left outer join + -- locations l on ti.location_id = l.id + left outer join + discounts d on ti.discount_id = d.id +where + t.created_at between '@{start_date}' and '@{end_date}' +-- AND +-- t.refunded IS NOT TRUE +order by t.created_at desc" \ No newline at end of file