子查询中的case语句后的BigQuery联接表

时间:2018-12-18 10:25:35

标签: google-bigquery standard-sql

我正在尝试编写一个可以每周或每月运行的查询,并可以在Google表格中进行一些数字更改。为此,我需要更改一些产品名称,以使它们更统一并与Google表格中的数据保持一致。在这种情况下,产品名称前的高级或初级名称应全部变为一个特定的名称,如“工程师”,而不是高级工程师,工程师,初级工程师等。但是,这在连接上产生了错误,例如不允许

我试图做到这一点的简短摘要代码如下(按等分组)被忽略了:

SELECT 
      t.fullVisitorId,
      t.product,
      t.transaction,
      t.transactiondate,
      cc.productvalue   
FROM t LEFT JOIN
`othertable` AS cc ON t.product = cc.product    
FROM(
SELECT
CASE 
WHEN product = '%engineer%' THEN 'Engineer'
END AS product,
fullVisitorId,
transaction,
value,
transactiondate
    FROM ( SELECT
     fullVisitorId,
              hit.transaction.transactionId AS transaction,
              prod.v2ProductName AS product,
           FORMAT_TIMESTAMP("%Y-%m-%d", TIMESTAMP_SECONDS(SAFE_CAST(visitStartTime AS INT64)), "Europe/London") AS transactiondate
            FROM
          `tableID.ga_sessions_*` AS t
        CROSS JOIN
          UNNEST(hits) AS hit            
        WHERE
          _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', '2018-11-01') 
          AND FORMAT_DATE("%E4Y%m%d", '2018-11-30')

1 个答案:

答案 0 :(得分:0)

以下是用于BigQuery标准SQL

SELECT 
  t.fullVisitorId,
  t.product,
  t.transaction,
  t.transactiondate,
  cc.productvalue   
FROM (
  SELECT CASE WHEN product LIKE '%engineer%' THEN 'Engineer' END AS product,
    fullVisitorId,
    transaction,
    value,
    transactiondate
  FROM ( 
    SELECT
      fullVisitorId,
      hit.transaction.transactionId AS transaction,
      prod.v2ProductName AS product,
      FORMAT_TIMESTAMP("%Y-%m-%d", TIMESTAMP_SECONDS(SAFE_CAST(visitStartTime AS INT64)), "Europe/London") AS transactiondate
    FROM `tableID.ga_sessions_*` AS t
    CROSS JOIN UNNEST(hits) AS hit            
    WHERE _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', '2018-11-01') 
    AND FORMAT_DATE("%E4Y%m%d", '2018-11-30')
  )
) AS t
LEFT JOIN `othertable` AS cc 
ON t.product = cc.product    

另外,请注意product LIKE '%engineer%'而不是product = '%engineer%'