结合两个多个bigquery SELECT FROM语句

时间:2015-06-04 18:12:25

标签: sql google-bigquery

我对BigQuery和SQL有些新手,所以我遇到的部分困难可能是我不知道如何描述我能够搜索答案的问题,但我看起来很温柔。

我要做的是计算满足一组条件的唯一userIds的总数,并将其除以唯一userIds的总数。

例如,要计算所有购买的唯一身份用户:

SELECT count(userId) 
FROM (SELECT userId 
FROM (FLATTEN([table1], user_attribute)) 
WHERE event_value > 0 and event_parameters.Name = "SKU" 
GROUP BY userId ORDER BY userId)

并计算唯一身份用户总数

 SELECT count(userId) 
 FROM (SELECT userId 
 FROM (FLATTEN([table1], user_attribute)) 
 GROUP BY userId ORDER BY userId)

我尝试将查询编写为

SELECT buyers/total 
    FROM (SELECT COUNT(userId) AS buyers 
    FROM (SELECT userId 
    FROM (FLATTEN([table1], user_attribute)) 
    WHERE event_value > 0 and event_parameters.Name = "SKU" 
    GROUP BY userId ORDER BY userId), 
COUNT(userId) as total
    FROM (SELECT userId 
    FROM (FLATTEN([table1], user_attribute)) 
    GROUP BY userId ORDER BY userId))

但它不起作用。我知道我在做一些根本错误的事情,但我不确定它是什么。我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:3)

您可以像这样统计不同的用户:

SELECT
  EXACT_COUNT_DISTINCT(userId) as buyers
FROM (FLATTEN([table1], user_attribute))
WHERE
  event_value > 0
  AND event_parameters.Name = "SKU"

加入它们的一种方法是添加静态标量值并将其用于连接:

SELECT
  buyers/total
FROM (
  SELECT
    EXACT_COUNT_DISTINCT(userId) AS buyers,
    1 AS scalar,
  FROM (FLATTEN([table1], user_attribute))
  WHERE
    event_value > 0
    AND event_parameters.Name = "SKU") a
JOIN (
  SELECT
    COUNT(userId) AS total,
    1 AS scalar,
  FROM (FLATTEN([table1], user_attribute)) ) b
ON
  a.scalar=b.scalar