BigQuery中的模糊列名称

时间:2016-10-31 19:28:57

标签: google-bigquery

我在下面有这样的陈述。当我在BigQuery中运行它时,我收到一个错误,指出小时是一个不明确的列名。我很确定我需要做的就是说明我需要几小时的表(hours.icxmedia-servers:icx_metrics.issues_and_zenhub),但是当我这样做并将其分组时,BigQuery说它是一个无效的组通过。我觉得我很接近,但我被困住了,任何建议都会非常有用

SELECT
  repo,
  ticket_number,
  title,
  hours,
  assignee,
  state,
  pipeline,
  created_at,
  closed_at,
  points,
  quarter_closed,
  year_closed, 
  CASE
    WHEN state = 'Closed' 
    THEN sum(hours)
    ELSE hours
    END AS hours
FROM (
SELECT
  repo,
  ticket_number,
  title,
  assignee,
  state,
  pipeline,
  MAX(IF(closed_at IS NOT NULL, 0, MAX(ROUND((end_epoch-start_epoch)/3600,2)))) AS hours,
  MIN(created_at) AS created_at,
  MAX(closed_at) AS closed_at,
  MAX(points) AS points,
  QUARTER(closed_at) AS quarter_closed,
  YEAR(closed_at) AS year_closed
FROM
  [icxmedia-servers:icx_metrics.issues_and_zenhub] AS historical,
  (
  SELECT
    repository.name AS repo,
    IF(issue.number IS NOT NULL, issue.number, pull_request.number) AS ticket_number,
    FIRST(IF(issue.number IS NOT NULL, issue.title, pull_request.title)) AS title,
    IF(issue.number IS NOT NULL, issue.assignee.login, pull_request.assignee.login) AS assignee,
pipeline.name AS pipeline,
    IF(MAX(IF(IF(issue.number IS NOT NULL, issue.state, pull_request.state) == "open",0,1)) == 1, "closed","open") AS state,
    IF(issue.number IS NOT NULL, issue.created_at, pull_request.created_at) AS created_at,
    MAX(IF(issue.number IS NOT NULL, issue.closed_at, pull_request.closed_at)) AS closed_at,
    NULL AS assign_times,
    TIMESTAMP_TO_SEC(IF(issue.updated_at IS NOT NULL,issue.updated_at, pull_request.updated_at)) AS start_epoch,
    LEAD(start_epoch, 1) OVER (ORDER BY ticket_number, start_epoch ASC) AS end_epoch,
    MAX(estimate.value) AS points,
    QUARTER(MAX(IF(issue.number IS NOT NULL, issue.closed_at, pull_request.closed_at))) AS quarter_closed,
    YEAR(MAX(IF(issue.number IS NOT NULL, issue.closed_at, pull_request.closed_at))) AS year_closed
  FROM
    [icxmedia-servers:icx_metrics.gh_zh_data_production]
  WHERE
    issue.number IS NOT NULL
    OR pull_request.number IS NOT NULL
  GROUP BY
    repo,
    start_epoch,
    pipeline,
    ticket_number,
    created_at,
    assignee) AS prod
    WHERE title=="LinkedIn"

GROUP BY
  repo,
  ticket_number,
  title,
  assignee,
  pipeline,
  state,
  quarter_closed,
  year_closed
) A
GROUP BY      
repo,
ticket_number,
title,
assignee,
state,
pipeline,
created_at,
closed_at,
points,
quarter_closed,
year_closed

1 个答案:

答案 0 :(得分:2)

在一个SELECT中有两个名为hours的字段,一个是直通hours字段,另一个是在CASE语句中计算的字段,也称为hours。重命名第二个以避免名称冲突。

SELECT
  ...
  hours,             <-- first 'hours'
  ...
  CASE
    WHEN state = 'Closed' 
    THEN sum(hours)
    ELSE hours
    END AS hours     <-- second 'hours'