SQL 包含联接的子查询

SQL Subquery containing Joins

我正在使用 Hive hql。我正在尝试在 issue_type='Impediments'

上对两个表进行内部连接过滤

现在我有一个新要求加入 dm_jira__label 以包含 labelissue_id 列。我尝试通过在 issue_id

上使用带有 dm_jira__label 的左连接来添加 issue_idlabel 的子查询
  INNER JOIN datamart_core.dm_jira__release
  ON dm_jira.issue_id =  dm_jira__release.issue_id;
    (   
        SELECT b.issue_id, b.label AS jira_label
        FROM datamart_core.dm_jira__label as B, datamart_core.dm_jira__release AS K
        LEFT JOIN b
        ON b.issue_id=k.issue_id
    );
  WHERE dm_jira.issue_type = 'Impediment') AS J

我收到以下错误:

AnalysisException: Illegal table reference to non-collection type: 'b' Path resolved to type: STRUCT<issue_id:DOUBLE,label:STRING>

查看下面的完整代码。提前致谢。

SELECT DISTINCT
j.project_key AS jira_project_key,
j.issue_type,
j.issue_assignee AS impediment_owner,
j.issue_status AS impediment_status,
j.issue_priority AS impediment_priority,
j.issue_summary AS impediment_summary,
j.`release` AS jira_release,
j.sow AS sow_num,
j.issue_due_date_utc AS jira_issue_due_date_utc,
j.issue_id AS jira_issue_id,
s.sow_family


from (
--Subquery to combine dm_jira and dm_jira__release
  SELECT dm_jira.project_key,
  dm_jira.issue_type,
  dm_jira.issue_assignee,
  dm_jira.issue_status,
  dm_jira.issue_priority,
  dm_jira.issue_summary,
  dm_jira.issue_due_date_utc,
  dm_jira.issue_id,
  dm_jira__release.`release`,
  dm_jira__release.sow

  from datamart_core.dm_jira


  INNER JOIN datamart_core.dm_jira__release
  ON dm_jira.issue_id =  dm_jira__release.issue_id;
    (   
        SELECT b.issue_id, b.label AS jira_label
        FROM datamart_core.dm_jira__label as B, datamart_core.dm_jira__release AS K
        LEFT JOIN b
        ON b.issue_id=k.issue_id
    );
  WHERE dm_jira.issue_type = 'Impediment') AS J


INNER JOIN datamart_core.dm_asoe_jira_scrum_summary AS S
ON j.`release` = s.jira_release
AND j.sow = s.sow_num
AND j.project_key = s.jira_project_key;

; 结束整个语句,不要在 sub-queries 的末尾使用它或加入。

使用无意义的别名,例如 BKJ 会损害可读性,不要这样做。

FROM x, yFROM x CROSS JOIN y 相同,它不是您要加入的 table 的列表。这意味着您有以下代码...

(   
  SELECT
     b.issue_id, b.label AS jira_label
  FROM
     datamart_core.dm_jira__label as B
  CROSS JOIN
     datamart_core.dm_jira__release AS K
  LEFT JOIN
     b
       ON b.issue_id=k.issue_id
)
  • LEFT JOIN 中的 b 不是 table,会导致语法错误。

然后,您的子查询就位于代码的中间,没有以任何方式连接或使用。我认为你想要一个更像这样的模式...

FROM
  datamart_core.dm_jira
INNER JOIN
  datamart_core.dm_jira__release
    ON dm_jira.issue_id =  dm_jira__release.issue_id;
LEFT JOIN
(   
  <your sub-query>
)
  AS fubar
    ON fubar.something = something.else
WHERE
  dm_jira.issue_type = 'Impediment'

即便如此,您实际上根本不需要嵌套 sub-queries。您可以继续添加联接,例如这样...

SELECT
  jira.project_key          AS jira_project_key,
  jira.issue_type,
  jira.issue_assignee       AS impediment_owner,
  jira.issue_status         AS impediment_status,
  jira.issue_priority       AS impediment_priority,
  jira.issue_summary        AS impediment_summary,
  jrel.`release`            AS jira_release,
  jrel.sow                  AS sow_num,
  jira.issue_due_date_utc   AS jira_issue_due_date_utc,
  jira.issue_id             AS jira_issue_id,
  jlab.label,
  summ.sow_family
FROM
  datamart_core.dm_jira                      AS jira
INNER JOIN
  datamart_core.dm_jira__release             AS jrel
    ON  jrel.issue_id = jira.issue_id
LEFT JOIN
  datamart_core.dm_jira__label               AS jlab
    ON  jlab.issue_id = jrel.issue_id
INNER JOIN
  datamart_core.dm_asoe_jira_scrum_summary   AS summ
    ON  summ.jira_release     = jrel.`release`
    AND summ.sow_num          = jrel.sow
    AND summ.jira_project_key = jira.project_key
WHERE
  jira.issue_type = 'Impediment'
;