2016-05-12 61 views
3

以下BigQuery SQL將檢索爲客戶購買的所有其他產品(fullVisitorId),該產品購買了名爲「大象「和一類」動物「。GA BigQuery - 購買產品x的用戶還購買了產品abc,然後僅篩選完整交易單,其中x是交易的一部分

現在是這個問題。如果同一個客戶(fullVisitorId)在同一日期範圍內發出兩個訂單。假設他或她在一次交易中購買了名爲「Tiger」的產品,再加上其他產品,然後是Tiger,那麼如果他或她在購買Elephant前一天或晚些時候做出類似訂單,這些產品也會顯示爲相關產品,因爲我的查詢是使用fullVisitorId。換句話說,一個fullVisitorId可以有一個或多個事務ID。一次交易對於當前交易購買的每件產品都具有相同的交易ID。

因此,有誰知道如何在最終購買主要產品Elephant時忽略所有其他使用不同transactionID的其他過濾器來僅顯示具有相同transactionId的相關產品的額外過濾功能嗎?

SELECT hits.product.productSKU AS other_purchased_products_sku, 
     hits.product.v2ProductName AS other_product_name, 
     hits.product.v2ProductCategory AS other_prod_category, 
     COUNT(hits.product.productSKU) AS quantity, 
     hits.transaction.transactionId AS trans_id, 
     fullVisitorId 
FROM 
    (SELECT fullVisitorId, 
      hits.product.productSKU, 
      hits.eCommerceAction.action_type, 
      hits.product.v2ProductName, 
      hits.product.v2ProductCategory, 
      hits.transaction.transactionId 
    FROM TABLE_DATE_RANGE([XXXXXX.ga_sessions_], TIMESTAMP('2016-04-1'), TIMESTAMP('2016-04-30'))) 
WHERE fullVisitorId IN 
    (SELECT fullVisitorId 
    FROM TABLE_DATE_RANGE([XXXXXX.ga_sessions_], TIMESTAMP('2016-04-1'), TIMESTAMP('2016-04-30')) 
    WHERE hits.product.v2ProductCategory CONTAINS 'Animals' 
     AND hits.product.v2ProductName = 'Elephant' 
     AND hits.eCommerceAction.action_type = '6' 
    GROUP BY fullVisitorId) 
    AND hits.product.v2ProductCategory CONTAINS 'Animals' 
    AND hits.product.v2ProductName IS NOT NULL 
    AND hits.product.v2ProductName !='Elephant' 
    AND hits.eCommerceAction.action_type = '6' 
GROUP BY other_purchased_products_sku, 
     other_product_name, 
     other_prod_category, 
     trans_id, 
     fullVisitorId 
ORDER BY trans_id DESC; 

回答

1

希望這是你所需要的:

SELECT 
    a.fv fv, 
    a.sku sku, 
    a.name name, 
    a.category category, 
    a.qtd qtd, 
    a.tid tid 
FROM(
    SELECT 
     fullvisitorid fv, 
     visitid v, 
     hits.product.productsku sku, 
     hits.product.v2productname name, 
     hits.product.v2productcategory category, 
     COUNT(hits.product.productsku) qtd, 
     hits.transaction.transactionid tid 
    FROM (table_date_range([XXXXXXX.ga_sessions_], timestamp("20160501"), timestamp("20160501"))), 
(table_date_range([XXXXXXXX.ga_sessions_intraday_], timestamp("20160501"), timestamp("20160501"))) 
    WHERE 1 = 1 
and hits.ecommerceaction.action_type = '6' 
GROUP EACH BY fv, v, sku, name, category, tid 
) a 
INNER JOIN EACH(
    SELECT 
     fullvisitorid fv, 
     visitid v, 
     hits.product.productsku sku, 
     hits.product.v2productname name, 
     hits.product.v2productcategory category, 
     hits.transaction.transactionid tid 
    FROM (table_date_range([XXXXXXXX.ga_sessions_], timestamp("20160501"), timestamp("20160501"))), 
(table_date_range([XXXXXXXX.ga_sessions_intraday_], timestamp("20160501"), timestamp("20160501"))) 
    WHERE 1 = 1 
and hits.ecommerceaction.action_type = '6' 
and lower(hits.product.v2productcategory) contains "animals" 
and lower(hits.product.v2productname) contains "elephant" 
    GROUP EACH BY fv, v, sku, name, category, tid 
) b 
on a.fv = b.fv and a.tid = b.tid 

其分解:

首先查詢組b

SELECT 
    fullvisitorid fv, 
    visitid v, 
    hits.product.productsku sku, 
    hits.product.v2productname name, 
    hits.product.v2productcategory category, 
    hits.transaction.transactionid tid 
FROM (table_date_range([XXXXXXXX.ga_sessions_], timestamp("20160501"), timestamp("20160501"))), 
    (table_date_range([XXXXXXXX.ga_sessions_intraday_], timestamp("20160501"), timestamp("20160501"))) 
WHERE 1 = 1 
    and hits.ecommerceaction.action_type = '6' 
    and lower(hits.product.v2productcategory) contains "animals" 
    and lower(hits.product.v2productname) contains "elephant" 
GROUP EACH BY fv, v, sku, name, category, tid 

此查詢首先將檢索fullvisitoridstransactionids你想要的類別animals和名稱elephant

然後在查詢組a

SELECT 
    fullvisitorid fv, 
    visitid v, 
    hits.product.productsku sku, 
    hits.product.v2productname name, 
    hits.product.v2productcategory category, 
    COUNT(hits.product.productsku) qtd, 
    hits.transaction.transactionid tid 
FROM (table_date_range([XXXXXXX.ga_sessions_], timestamp("20160501"), timestamp("20160501"))), 
(table_date_range([XXXXXXXX.ga_sessions_intraday_], timestamp("20160501"), timestamp("20160501"))) 
WHERE 1 = 1 
    and hits.ecommerceaction.action_type = '6' 
GROUP EACH BY fv, v, sku, name, category, tid 

您檢索所有fullvisitorids,他們transactions和記者products

由於內部連接只有您感興趣的交易仍然存在(在給定交易中購買「動物」類別和名稱「大象」的全職顧問)。

我希望這會有所幫助。任何問題讓我知道,

+0

嗨請問,謝謝你解決問題和質量的解釋。你也知道我可以如何添加兩個額外的列。 transaction_ga:date(YYYYMMDD),transaction_ga:isoWeek_number(1-52(如果第53周存在,則爲3)?)? –

+0

只是在每個組內加入'date'和'week(date)'這兩個字段,並在這些字段上加入連接。 –

+0

也行得通。再次感謝您對此的支持。 –