sql 脚本适用于 MySQL,但不适用于 google bigquery
Posted
技术标签:
【中文标题】sql 脚本适用于 MySQL,但不适用于 google bigquery【英文标题】:sql script works on MySQL, but not on google bigquery 【发布时间】:2013-02-27 20:34:26 【问题描述】:我有在 mysql 中运行良好的 sql 脚本,但我无法在 google bigquery 中运行。通读 bq 文档后,我做了一些调整(例如,每个 select 语句不超过一个 join),但脚本仍然失败。任何帮助表示赞赏。如果您知道 bq sql 与其他 sql 方面的任何好的资源,那也将不胜感激。谢谢。
SELECT
T1.action_date AS action_date,
T1.ad_campaign_category AS ad_campaign_category,
T1.campaign_id AS campaign_id,
T2.total_sends AS total_sends,
count(*) AS clicks_per_category
FROM (
SELECT action_date, campaign_id, ad_campaign_category
FROM projectX.email_action
WHERE action_date > '2009-04-01' AND action_date < '2011-05-01') T1,
(
SELECT action_date, campaign_id, ad_campaign_category, count(*) AS total_sends
FROM projectX.email_action
WHERE action_type = 'send' AND action_date > '2009-04-01' AND action_date < '2011-05-01'
GROUP BY action_date, campaign_id) T2
WHERE T1.action_date = T2.action_date
AND T1.campaign_id = T2.campaign_id
GROUP BY action_date, campaign_id, ad_campaign_category
【问题讨论】:
您是否遇到了特定的错误消息? 下面的答案纠正了这个问题。这是错误消息:错误:未知字段:T1.action_date 【参考方案1】:JOIN 必须是显式的——也就是说,您应该使用SELECT ... FROM (...) t1 JOIN (...) t2 ON t1.x = t2.y
的形式,而不是使用SELECT ... FROM (...) t1, (...) t2 WHERE t1.x = t2.y
对于您的示例,这看起来像:
SELECT
T1.action_date AS action_date,
T1.ad_campaign_category AS ad_campaign_category,
T1.campaign_id AS campaign_id,
T2.total_sends AS total_sends,
count(*) AS clicks_per_category
FROM (
SELECT action_date, campaign_id, ad_campaign_category
FROM projectX.email_action
WHERE action_date > '2009-04-01' AND action_date < '2011-05-01') T1
JOIN (
SELECT action_date, campaign_id, ad_campaign_category, count(*) AS total_sends
FROM projectX.email_action
WHERE action_type = 'send' AND action_date > '2009-04-01' AND action_date < '2011-05-01'
GROUP BY action_date, campaign_id) T2
ON T1.action_date = T2.action_date
AND T1.campaign_id = T2.campaign_id
GROUP BY action_date, campaign_id, ad_campaign_category
请注意,如果您收到一个表太大的错误,请尝试使用JOIN EACH
而不是JOIN
。
【讨论】:
以上是关于sql 脚本适用于 MySQL,但不适用于 google bigquery的主要内容,如果未能解决你的问题,请参考以下文章
查询适用于 phpMyAdmin,但不适用于 Java [重复]
import pymongo 适用于 Python 解释器,但不适用于脚本