无法按BigQuery / Firebase中的内容对购买者进行排序

时间:2017-06-30 12:29:42

标签: sql firebase google-bigquery firebase-analytics

我使用Firebase分析移动应用。来自Firebase的数据将导出到BigQuery。

我尝试使用以标准SQL编写的以下查询来计算与特定内容相关的购买者:

SELECT COUNT(user_dim.app_info.app_instance_id) AS purchasers
    ,eventParams.value.string_value AS eventName
    ,event.DATE AS eventDate
FROM `id.TABLE.app_events_ * `
    ,UNNEST(event_dim) AS event
    ,UNNEST(event.params) AS eventParams
WHERE (
        event.NAME = 'select_content'
        AND eventParams.KEY = 'item_id'
        )
    AND user_dim.app_info.app_instance_id IN (
        SELECT [user_dim.app_info.app_instance_id]
        FROM `id.TABLE.app_events_ * `
            ,UNNEST(event_dim) AS event
            ,UNNEST(event.params) AS eventParams
        WHERE event.NAME = 'in_app_purchase'
            AND eventParams.KEY = 'quantity'
        )
GROUP BY eventDate
    ,eventName
ORDER BY eventDate DESC

但是我收到错误消息:'无法使用无法比较的类型执行IN子查询STRING和ARRAY'

我试图使用' UNNEST' ' IN'之后的功能运营商。 但后来我得到了错误:' Scalar子查询产生了多个元素'

请帮我找出我的错误。

3 个答案:

答案 0 :(得分:2)

您只需删除订单中的括号即可使查询正常工作:

AND user_dim.app_info.app_instance_id IN (
    SELECT user_dim.app_info.app_instance_id
    FROM `id.TABLE.app_events_ * `
        ,UNNEST(event_dim) AS event
        ,UNNEST(event.params) AS eventParams
    WHERE event.NAME = 'in_app_purchase'
        AND eventParams.KEY = 'quantity'
    )

问题是您是通过在列名称周围放置括号来创建数组的。

答案 1 :(得分:0)

也许您可以在消除此错误的同时优化查询,如下所示:

WITH data AS(
  SELECT STRUCT<user_id STRING, app_info STRUCT<app_instance_id STRING> > ('user_1', STRUCT('id_1' as app_instance_id)) user_dim, ARRAY<STRUCT<name STRING, date STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING> >> > > [STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value1' as string_value) as value), STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('in_app_purchase' as name, "20170610" as date, [STRUCT('quantity' as key, STRUCT('str_value1' as string_value) as value)] as params), STRUCT('select_content' as name, "20170611" as date, [STRUCT('item_id' as key, STRUCT('str_value1' as string_value) as value)] as params), STRUCT('select_content' as name, "20170611" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params)] event_dim UNION ALL
  SELECT STRUCT<user_id STRING, app_info STRUCT<app_instance_id STRING> > ('user_2', STRUCT('id_2' as app_instance_id)) user_dim, ARRAY<STRUCT<name STRING, date STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING> >> > > [STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value1' as string_value) as value), STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('in_app_purchase' as name, "20170610" as date, [STRUCT('quantity' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('select_content' as name, "20170612" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params)] event_dim union all
  SELECT STRUCT<user_id STRING, app_info STRUCT<app_instance_id STRING> > ('user_2', STRUCT('id_2' as app_instance_id)) user_dim, ARRAY<STRUCT<name STRING, date STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING> >> > > [STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value1' as string_value) as value)] as params), STRUCT('select_content' as name, "20170612" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params)] event_dim
)


SELECT
  date,
  eventName,
  COUNT(1) purchases
FROM(
  SELECT
  ARRAY(SELECT AS STRUCT event.date date, params.value.string_value eventName FROM UNNEST(event_dim) event, UNNEST(event.params) params GROUP BY date, eventName) data
FROM data
WHERE EXISTS(SELECT 1 FROM UNNEST(event_dim) event WHERE event.name = 'in_app_purchase' AND (SELECT COUNT(1) FROM UNNEST(event.params) params WHERE params.key = 'quantity') > 0)
),
UNNEST(data)
GROUP BY
  date, eventName

它不仅应该运行得更快,而且还应该更便宜,因为它会查询较少的数据列。

答案 2 :(得分:0)

我创建了查询,帮助我按内容对购买者进行排序。它在Legacy SQL中:

`

SELECT
  user_dim.app_info.app_platform as appPlatform,
  event_dim.date as eventDate,
  COUNT(event_dim.name) as purchasers,
  user_dim.app_info.app_instance_id as appInstanceID,
  event_dim.params.value.string_value as contentID,
  event_dim.value_in_usd as revenue,
  CASE
    WHEN event_dim.params.value.string_value = 'content id1'  THEN 'content name1'
    WHEN event_dim.params.value.string_value = 'content id2'  THEN 'content name2'
    ELSE 'Undefined'
  END as contentName
FROM
  TABLE_QUERY([table_IOS], 'table_id CONTAINS "app_events_"'),
  TABLE_QUERY([table_ANDROID], 'table_id CONTAINS "app_events_"')
WHERE`enter code here`
  (event_dim.name = 'in_app_purchase' AND event_dim.params.key = 'product_id' OR event_dim.params.key = 'price')
  AND event_dim.params.value.string_value != 'null'
GROUP BY
  appPlatform, eventDate, appInstanceID, contentID, revenue, contentName 
ORDER BY
  appPlatform, eventDate DESC

`