BigQuery API:400语法错误:预期关键字JOIN但收到“)”

时间:2019-01-13 12:19:08

标签: pandas api google-bigquery

我正在尝试通过bq python api(在标准SQL模式下)运行查询,但是出现错误:

400语法错误:预期关键字JOIN但在[1:621]处获得了“)”

这是我的代码:

    from google.cloud import bigquery
    import pandas as pd
    client = bigquery.Client.from_service_account_json(r'/Users/dmitrij/Desktop/api-*****.json')
    QUERY2=("select date,pagePath,prev_page_path,hits.eventInfo.eventCategory,hits.eventInfo.eventAction,hits.eventInfo.eventLabel, COUNT(CONCAT(prev_page_path,pagePath,hits.eventInfo.eventAction,hits.eventInfo.eventLabel)) as count from (SELECT hits.page.pagePath AS pagePath, LAG(hits.page.pagePath) OVER (PARTITION BY fullVisitorId, visitStartTime ORDER BY hits.hitNumber) AS prev_page_path, date, hits.eventInfo.eventCategory, hits.eventInfo.eventAction, hits.eventInfo.eventLabel FROM (TABLE_DATE_RANGE([api-open-broker.150225190.ga_sessions_], DATE_ADD(CURRENT_TIMESTAMP(), -8, 'DAY'), DATE_ADD(CURRENT_TIMESTAMP(), -1, 'DAY')))")
    query_job2 = client.query(QUERY2)
    df_prevp = query_job2.to_dataframe()

QUERY2使用旧版SQL语法,但如果将代码放在前面,例如:

job_config = bigquery.QueryJobConfig()
job_config.use_legacy_sql = True

我有一个错误:

400在第1行第621列遇到“”。 期望:     “)” ...

这是QUERY2:

select date,pagePath,prev_page_path,hits.eventInfo.eventCategory,hits.eventInfo.eventAction,hits.eventInfo.eventLabel, COUNT(CONCAT(prev_page_path,pagePath,hits.eventInfo.eventAction,hits.eventInfo.eventLabel)) as count
from
(SELECT
 hits.page.pagePath AS pagePath,
 LAG(hits.page.pagePath) OVER (PARTITION BY fullVisitorId, visitStartTime ORDER BY hits.hitNumber) AS prev_page_path,
 date,
 hits.eventInfo.eventCategory,
 hits.eventInfo.eventAction,
 hits.eventInfo.eventLabel
   FROM
 (TABLE_DATE_RANGE([api-open-broker.150225190.ga_sessions_],
 DATE_ADD(CURRENT_TIMESTAMP(), -8, 'DAY'),         DATE_ADD(CURRENT_TIMESTAMP(), -1, 'DAY')))
   WHERE
 hits.type="EVENT"
 AND hits.eventInfo.eventCategory LIKE "%Title_Name_Podpisat%" )
where prev_page_path is not null 
group by pagePath,prev_page_path,date,hits.eventInfo.eventCategory,hits.eventInfo.eventAction,hits.eventInfo.eventLabel

如何避免此错误?

1 个答案:

答案 0 :(得分:1)

此错误

  

400在第1行的第621列遇到了“”。期望:“)” ...

意味着您没有设置job_config并且BQ尝试将查询作为标准sql运行,我希望看到这样的内容:

Pascal's triangle

另一种选择是在代码开头使用query_job2 = client.query(QUERY2, job_config=job_config)