Skip to content

Commit b91c026

Browse files
committed
docs: normalize categorical groupings in SQL query library
1 parent 1bfe9d9 commit b91c026

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

data-activation/template-resources/sql-query-library.mdx

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Most examples default to the last 30 days for performance and "current state" an
5151
-- Assumptions: timeframe=last_30_days | metric=CAC=ad_spend/new_customer_count | grain=sm_channel | scope=all_channels
5252
WITH channel_rollup AS (
5353
SELECT
54-
sm_channel,
54+
COALESCE(NULLIF(LOWER(TRIM(sm_channel)), ''), '(unknown)') AS sm_channel,
5555
SUM(ABS(ad_spend)) AS ad_spend,
5656
SUM(new_customer_count) AS new_customers
5757
FROM `your_project.sm_transformed_v2.rpt_executive_summary_daily`
@@ -98,8 +98,8 @@ Most examples default to the last 30 days for performance and "current state" an
9898
-- Assumptions: timeframe=last_30_days | metric=ROAS=platform_reported_revenue/ad_spend | grain=platform+campaign_type | scope=all_stores
9999
SELECT
100100
sm_store_id,
101-
source_system AS platform,
102-
ad_campaign_type AS campaign_type,
101+
COALESCE(NULLIF(LOWER(TRIM(source_system)), ''), '(unknown)') AS platform,
102+
COALESCE(NULLIF(LOWER(TRIM(ad_campaign_type)), ''), '(unknown)') AS campaign_type,
103103
SUM(ad_platform_reported_revenue) AS platform_reported_revenue,
104104
SUM(ad_spend) AS ad_spend,
105105
SAFE_DIVIDE(SUM(ad_platform_reported_revenue), NULLIF(SUM(ad_spend), 0)) AS roas
@@ -120,7 +120,7 @@ Most examples default to the last 30 days for performance and "current state" an
120120
WITH monthly AS (
121121
SELECT
122122
DATE_TRUNC(date, MONTH) AS month_start,
123-
source_system AS platform,
123+
COALESCE(NULLIF(LOWER(TRIM(source_system)), ''), '(unknown)') AS platform,
124124
SUM(ad_platform_reported_revenue) AS platform_reported_revenue,
125125
SUM(ad_spend) AS ad_spend
126126
FROM `your_project.sm_transformed_v2.rpt_ad_performance_daily`
@@ -150,8 +150,8 @@ Most examples default to the last 30 days for performance and "current state" an
150150
```sql
151151
-- Assumptions: timeframe=last_30_days | metric=engagement+platform_attributed_orders_revenue | grain=channel+message_type | scope=all_messages
152152
SELECT
153-
COALESCE(sm_message_channel, '(unknown)') AS sm_message_channel,
154-
COALESCE(message_type, '(unknown)') AS message_type,
153+
COALESCE(NULLIF(LOWER(TRIM(sm_message_channel)), ''), '(unknown)') AS sm_message_channel,
154+
COALESCE(NULLIF(LOWER(TRIM(message_type)), ''), '(unknown)') AS message_type,
155155
SUM(message_unique_receives) AS receives,
156156
SUM(message_unique_opens) AS opens,
157157
SUM(message_unique_clicks) AS clicks,
@@ -181,10 +181,10 @@ Most examples default to the last 30 days for performance and "current state" an
181181
```sql
182182
-- Assumptions: timeframe=last_30_days | metric=platform_attributed_orders_revenue | grain=campaign | scope=campaigns_only
183183
SELECT
184-
sm_message_channel,
185-
source_system,
184+
COALESCE(NULLIF(LOWER(TRIM(sm_message_channel)), ''), '(unknown)') AS sm_message_channel,
185+
COALESCE(NULLIF(LOWER(TRIM(source_system)), ''), '(unknown)') AS source_system,
186186
campaign_id,
187-
campaign_name,
187+
ANY_VALUE(campaign_name) AS campaign_name,
188188
SUM(message_unique_receives) AS receives,
189189
SUM(message_unique_opens) AS opens,
190190
SUM(message_unique_clicks) AS clicks,
@@ -195,7 +195,7 @@ Most examples default to the last 30 days for performance and "current state" an
195195
FROM `your_project.sm_transformed_v2.rpt_outbound_message_performance_daily`
196196
WHERE date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
197197
AND LOWER(message_type) = 'campaign'
198-
GROUP BY 1, 2, 3, 4
198+
GROUP BY 1, 2, 3
199199
ORDER BY platform_reported_order_revenue DESC
200200
LIMIT 25;
201201
```
@@ -208,7 +208,7 @@ Most examples default to the last 30 days for performance and "current state" an
208208
-- Assumptions: timeframe=last_12_weeks | metric=list_subscribes_unsubscribes_net | grain=week+channel | scope=all_messages
209209
SELECT
210210
DATE_TRUNC(date, WEEK(MONDAY)) AS week_start,
211-
COALESCE(sm_message_channel, '(unknown)') AS sm_message_channel,
211+
COALESCE(NULLIF(LOWER(TRIM(sm_message_channel)), ''), '(unknown)') AS sm_message_channel,
212212
SUM(list_subscribes) AS list_subscribes,
213213
SUM(list_unsubscribes) AS list_unsubscribes,
214214
SUM(list_subscribes) - SUM(list_unsubscribes) AS net_list_growth,
@@ -226,9 +226,9 @@ Most examples default to the last 30 days for performance and "current state" an
226226
```sql
227227
-- Assumptions: timeframe=last_30_days | metric=engagement+platform_attributed_orders_revenue | grain=provider+channel+message_type | scope=message_sends_only
228228
SELECT
229-
COALESCE(source_system, '(unknown)') AS source_system,
230-
COALESCE(sm_message_channel, '(unknown)') AS sm_message_channel,
231-
COALESCE(message_type, '(unknown)') AS message_type,
229+
COALESCE(NULLIF(LOWER(TRIM(source_system)), ''), '(unknown)') AS source_system,
230+
COALESCE(NULLIF(LOWER(TRIM(sm_message_channel)), ''), '(unknown)') AS sm_message_channel,
231+
COALESCE(NULLIF(LOWER(TRIM(message_type)), ''), '(unknown)') AS message_type,
232232
SUM(message_unique_receives) AS receives,
233233
SUM(message_unique_opens) AS opens,
234234
SUM(message_unique_clicks) AS clicks,
@@ -280,9 +280,9 @@ Most examples default to the last 30 days for performance and "current state" an
280280
-- Assumptions: timeframe=last_12_weeks | metric=bounce_rate+drop_rate | grain=week+provider+channel+message_type | scope=message_sends_only
281281
SELECT
282282
DATE_TRUNC(date, WEEK(MONDAY)) AS week_start,
283-
COALESCE(source_system, '(unknown)') AS source_system,
284-
COALESCE(sm_message_channel, '(unknown)') AS sm_message_channel,
285-
COALESCE(message_type, '(unknown)') AS message_type,
283+
COALESCE(NULLIF(LOWER(TRIM(source_system)), ''), '(unknown)') AS source_system,
284+
COALESCE(NULLIF(LOWER(TRIM(sm_message_channel)), ''), '(unknown)') AS sm_message_channel,
285+
COALESCE(NULLIF(LOWER(TRIM(message_type)), ''), '(unknown)') AS message_type,
286286
SUM(message_unique_receives) AS receives,
287287
SUM(message_unique_bounces) AS bounces,
288288
SUM(message_unique_drops) AS drops,
@@ -303,14 +303,14 @@ Most examples default to the last 30 days for performance and "current state" an
303303
```sql
304304
-- Assumptions: timeframe=last_30_days | metric=click_rate+platform_attributed_outcomes | grain=message | scope=min_receives_threshold
305305
SELECT
306-
COALESCE(source_system, '(unknown)') AS source_system,
307-
COALESCE(sm_message_channel, '(unknown)') AS sm_message_channel,
308-
COALESCE(message_type, '(unknown)') AS message_type,
306+
COALESCE(NULLIF(LOWER(TRIM(source_system)), ''), '(unknown)') AS source_system,
307+
COALESCE(NULLIF(LOWER(TRIM(sm_message_channel)), ''), '(unknown)') AS sm_message_channel,
308+
COALESCE(NULLIF(LOWER(TRIM(message_type)), ''), '(unknown)') AS message_type,
309309
message_id,
310-
message_name,
311-
message_subject,
310+
ANY_VALUE(message_name) AS message_name,
311+
ANY_VALUE(message_subject) AS message_subject,
312312
campaign_id,
313-
campaign_name,
313+
ANY_VALUE(campaign_name) AS campaign_name,
314314
SUM(message_unique_receives) AS receives,
315315
SUM(message_unique_opens) AS opens,
316316
SUM(message_unique_clicks) AS clicks,
@@ -320,7 +320,7 @@ Most examples default to the last 30 days for performance and "current state" an
320320
FROM `your_project.sm_transformed_v2.rpt_outbound_message_performance_daily`
321321
WHERE date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
322322
AND message_id IS NOT NULL
323-
GROUP BY 1, 2, 3, 4, 5, 6, 7, 8
323+
GROUP BY 1, 2, 3, 4, 7
324324
HAVING receives >= 10000
325325
ORDER BY click_rate DESC, receives DESC
326326
LIMIT 25;
@@ -433,7 +433,7 @@ Most examples default to the last 30 days for performance and "current state" an
433433
```sql
434434
-- Assumptions: timeframe=last_30_days | metric=funnel_step_ratios | grain=source_system | scope=event_based_monitoring
435435
SELECT
436-
COALESCE(source_system, '(unknown)') AS source_system,
436+
COALESCE(NULLIF(LOWER(TRIM(source_system)), ''), '(unknown)') AS source_system,
437437
SUM(page_view_event_count) AS page_views,
438438
SUM(view_item_event_count) AS view_item_events,
439439
SUM(add_to_cart_event_count) AS add_to_cart_events,
@@ -459,7 +459,7 @@ Most examples default to the last 30 days for performance and "current state" an
459459
WITH hourly AS (
460460
SELECT
461461
event_local_datetime AS hour_start,
462-
COALESCE(source_system, '(unknown)') AS source_system,
462+
COALESCE(NULLIF(LOWER(TRIM(source_system)), ''), '(unknown)') AS source_system,
463463
SUM(purchase_event_count) AS purchase_events,
464464
SUM(event_order_revenue) AS event_order_revenue
465465
FROM `your_project.sm_transformed_v2.rpt_funnel_events_performance_hourly`
@@ -920,7 +920,7 @@ Most examples default to the last 30 days for performance and "current state" an
920920
```sql
921921
-- Assumptions: timeframe=last_30_days | metric=orders+net_revenue+share | grain=sm_channel | scope=valid_orders_only
922922
SELECT
923-
sm_channel,
923+
COALESCE(NULLIF(LOWER(TRIM(sm_channel)), ''), '(unknown)') AS sm_channel,
924924
COUNT(*) AS orders,
925925
SUM(order_net_revenue) AS order_net_revenue,
926926
SAFE_DIVIDE(COUNT(*), NULLIF(SUM(COUNT(*)) OVER (), 0)) AS pct_orders,
@@ -929,7 +929,6 @@ Most examples default to the last 30 days for performance and "current state" an
929929
WHERE is_order_sm_valid = TRUE
930930
AND order_cancelled_at IS NULL
931931
AND DATE(order_processed_at_local_datetime) >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
932-
AND sm_channel IS NOT NULL
933932
GROUP BY 1
934933
ORDER BY orders DESC;
935934
```
@@ -2259,17 +2258,17 @@ These are deeper-dive investigations for when attribution looks “weird” (too
22592258
-- Assumptions: timeframe=last_90_days | metric=unattributed_share | grain=source_system+sm_channel | scope=valid_orders_only
22602259
WITH base AS (
22612260
SELECT
2262-
source_system,
2263-
sm_channel,
2261+
COALESCE(NULLIF(LOWER(TRIM(source_system)), ''), '(unknown)') AS source_system,
2262+
COALESCE(NULLIF(LOWER(TRIM(sm_channel)), ''), '(unknown)') AS sm_channel,
22642263
COALESCE(NULLIF(LOWER(TRIM(sm_utm_source_medium)), ''), '(none) / (none)') AS source_medium,
22652264
sm_order_key,
22662265
order_net_revenue
22672266
FROM `your_project.sm_transformed_v2.obt_orders`
22682267
WHERE is_order_sm_valid = TRUE
22692268
AND order_cancelled_at IS NULL
22702269
AND DATE(order_processed_at_local_datetime) >= DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY)
2271-
AND source_system IS NOT NULL
2272-
AND sm_channel IS NOT NULL
2270+
AND NULLIF(LOWER(TRIM(source_system)), '') IS NOT NULL
2271+
AND NULLIF(LOWER(TRIM(sm_channel)), '') IS NOT NULL
22732272
)
22742273
SELECT
22752274
source_system,
@@ -2517,7 +2516,7 @@ These are deeper-dive investigations for when attribution looks “weird” (too
25172516
```sql
25182517
-- Assumptions: timeframe=last_30_days | metric=ticket_volume+one_touch_rate | grain=communication_channel | scope=exclude_spam
25192518
SELECT
2520-
COALESCE(NULLIF(ticket_communication_channel, ''), '(unknown)') AS ticket_communication_channel,
2519+
COALESCE(NULLIF(LOWER(TRIM(ticket_communication_channel)), ''), '(unknown)') AS ticket_communication_channel,
25212520
COUNT(DISTINCT sm_ticket_key) AS tickets,
25222521
COUNTIF(ticket_status IS NOT NULL AND LOWER(ticket_status) = 'open') AS open_tickets,
25232522
COUNTIF(ticket_status IS NOT NULL AND LOWER(ticket_status) = 'closed') AS closed_tickets,
@@ -2667,8 +2666,8 @@ These are deeper-dive investigations for when attribution looks “weird” (too
26672666
```sql
26682667
-- Assumptions: timeframe=last_30_days | metric=ticket_volume+resolution_time | grain=priority+channel+team | scope=exclude_spam
26692668
SELECT
2670-
COALESCE(NULLIF(ticket_priority, ''), '(unknown)') AS ticket_priority,
2671-
COALESCE(NULLIF(ticket_communication_channel, ''), '(unknown)') AS ticket_communication_channel,
2669+
COALESCE(NULLIF(LOWER(TRIM(ticket_priority)), ''), '(unknown)') AS ticket_priority,
2670+
COALESCE(NULLIF(LOWER(TRIM(ticket_communication_channel)), ''), '(unknown)') AS ticket_communication_channel,
26722671
COALESCE(NULLIF(ticket_assignee_team_name, ''), '(unassigned)') AS ticket_assignee_team_name,
26732672
COUNT(DISTINCT sm_ticket_key) AS tickets,
26742673
COUNTIF(ticket_status IS NOT NULL AND LOWER(ticket_status) = 'open') AS open_tickets,

0 commit comments

Comments
 (0)