如何在Google Big Query

时间:2017-01-24 17:26:17

标签: google-bigquery

有没有办法在不使用Max功能的情况下在GBQ中使用多个自定义尺寸?我使用Max函数的问题是它只保存了最大pax_num,但我希望得到所有组合的访问者数(Date,product.v2ProductCategory,eCommerceAction.action_type) ,product.v2ProductName)。请注意,pax_num是该票证上的pax数。我需要dest + pax_num的每个组合,而不是dest + max(pax_num)

SELECT
    Date
  ,count(distinct( concat(FULLVISITORID,cast(visitID as string)))) as visitor
 , product.v2ProductCategory as product_category

  ,max(if(customDimensions.index=2, customDimensions.value,null))  as dest
  ,max((if(customDimensions.index=21, customDimensions.value,null)) ) as pax_num
 ,eCommerceAction.action_type as Action_type

 ,product.v2ProductName as product_name
FROM `table` as t
  CROSS JOIN UNNEST(hits) AS hit
  CROSS JOIN UNNEST(hit.customDimensions) AS customDimensions
  CROSS JOIN UNNEST(hit.product) AS product

GROUP BY 
      Date
      ,product.v2ProductCategory
    ,eCommerceAction.action_type
,product.v2ProductName

2 个答案:

答案 0 :(得分:0)

不确定这是否是您要找的,但是如果您在组中包含字段 pax_num ,您可能已经找到了所需内容,例如:

select
    date,
    count(distinct( concat(FULLVISITORID,cast(visitID as string)))) as sessions,
    product.v2ProductCategory category,
    max(if(customDimensions.index=2, customDimensions.value, null)) as dest,
    if(customDimensions.index=21, customDimensions.value,null) as pax_num,
    eCommerceAction.action_type as act_type,
    product.v2ProductName as product_name
from `table` as t,
unnest(hits) as hit,
unnest(hit.customDimensions) customDimensions,
unnest(hit.product) as product
group by
    date,
    category,
    act_type,
    pax_num,
    product_name
having pax_num is not null

您举例说明了pax_num值" paxnum_5"和" paxnum_6"。如果您在pax_num操作中插入值group bycount聚合应该在pax_num级别上发生,这将保留值(而不是将所有内容混合到max中价值与以前一样)。

另外,请注意,如果计算 fullvisitorids visitids 的独特组合,您实际上是在计算会话总数而不是访问者(他们的定义不一样) )。

答案 1 :(得分:0)

添加fullvisitorID解决问题

SELECT
    Date
,concat(fullVisitorID,cast(visitID as string)) as visitorID
  ,count(distinct( concat(FULLVISITORID,cast(visitID as string)))) as visitor
 , product.v2ProductCategory as product_category

  ,max(if(customDimensions.index=2, customDimensions.value,null))  as dest
  ,max((if(customDimensions.index=21, customDimensions.value,null)) ) as pax_num
 ,eCommerceAction.action_type as Action_type

 ,product.v2ProductName as product_name
FROM `table` as t
  CROSS JOIN UNNEST(hits) AS hit
  CROSS JOIN UNNEST(hit.customDimensions) AS customDimensions
  CROSS JOIN UNNEST(hit.product) AS product

GROUP BY 
      Date
      ,product.v2ProductCategory
    ,eCommerceAction.action_type
,product.v2ProductName
,visitorID