BigQuery说查询有效,但返回'找不到字段客户ID'

时间:2016-05-31 21:05:45

标签: google-bigquery

这是我正在运行的查询 - 正如我所说,BigQuery说它有效但在我运行查询后返回错误。我在Postgres中运行此查询(对不同的UNION ALL语法进行必要的更改)并且它按预期工作。任何帮助将不胜感激,因为我对BQ相对较新。感谢。

错误消息

Query Failed
Error: Field 'customerid' not found.

查询

SELECT jobs.customerid,
       jobs.total,
       jobs.count,
       city_max.city
FROM
  (SELECT customerid,
          sum(total) AS total,
          count(*) AS count
   FROM [events.transaction_20160301],[events.transaction_20160401] group by customerid ) AS jobs
JOIN 
  (SELECT customer_city_count.customerid,
          city,
          city_count
   FROM
     (SELECT customerid,
             city,
             count(*) AS city_count
      FROM [events.transaction_20160301],[events.transaction_20160401]

      GROUP BY customerid, city) AS customer_city_count
    JOIN
     (SELECT customerid,
             max(ct) AS max_ct
      FROM
        (SELECT customerid,
                city,
                count(*) AS ct
         FROM [events.transaction_20160301],[events.transaction_20160401] 
         GROUP BY customerid, city)
      GROUP BY customerid) AS customer_max_city ON customer_max_city.customerid = customer_city_count.customerid
   AND city_count = customer_max_city.max_ct) AS city_max ON city_max.customerid = jobs.customerid

1 个答案:

答案 0 :(得分:2)

BigQuery对别名/字段命名非常敏感 请尝试以下

SELECT jobs.customerid,
       jobs.total,
       jobs.count,
       city_max.city
FROM
  (SELECT customerid,
          SUM(total) AS total,
          COUNT(*) AS COUNT
   FROM [events.transaction_20160301],[events.transaction_20160401] GROUP BY customerid ) AS jobs
JOIN 
  (SELECT customer_city_count.customerid as customerid,
          city,
          city_count
   FROM
     (SELECT customerid,
             city,
             COUNT(*) AS city_count
      FROM [events.transaction_20160301],[events.transaction_20160401]

      GROUP BY customerid, city) AS customer_city_count
    JOIN
     (SELECT customerid,
             MAX(ct) AS max_ct
      FROM
        (SELECT customerid,
                city,
                COUNT(*) AS ct
         FROM [events.transaction_20160301],[events.transaction_20160401] 
         GROUP BY customerid, city)
      GROUP BY customerid) AS customer_max_city ON customer_max_city.customerid = customer_city_count.customerid
   AND city_count = customer_max_city.max_ct) AS city_max ON city_max.customerid = jobs.customerid  

下面的行只有一处变化

SELECT customer_city_count.customerid as customerid