Sql 使用多表联接创建BigQuery语句

Sql 使用多表联接创建BigQuery语句,sql,join,google-bigquery,Sql,Join,Google Bigquery,我在BigQuery上遇到了麻烦,我想我在某个地方遗漏了一部分: SELECT timestamp, REGEXP_EXTRACT(Analyte.id,r'^Patient, (\d+)') as patientId, Analyte.contextId as contextId, Analyte.analyteServiceCode as analyteServiceCode, Analyte.resultRefRangeLow as ana

我在BigQuery上遇到了麻烦,我想我在某个地方遗漏了一部分:

SELECT 
    timestamp, 
    REGEXP_EXTRACT(Analyte.id,r'^Patient, (\d+)') as patientId, 
    Analyte.contextId as contextId, 
    Analyte.analyteServiceCode as analyteServiceCode, 
    Analyte.resultRefRangeLow as analyteLowValue, 
    Analyte.resultRefRangeHigh as analyteHighValue, 
    Analyte.resultValue as resultValue 
FROM [lustgarten_sandbox.Analyte] as Analyte 
join (
    SELECT 
        AnalyteMapping.CONTEXTID as contextId, 
        AnalyteMapping.CODE as analyteServiceCode, 
        AnalyteName.NAME
    FROM [lustgarten_sandbox.AnalyteMapping] as AnalyteMapping 
    join [lustgarten_sandbox.AnalyteName] as AnalyteName
    on AnalyteName.id = AnalyteMapping.id
    where upper(AnalyteName.NAME) = 'Analyte' ) as SpecificAnalyte 
on  SpecificAnalyte.contextId = Analyte.contextId 
    and SpecificAnalyte.analyteServiceCode = Analyte.analyteServiceCode
join 
SELECT 
    ldePatientId as ptID, 
    origBreedCode, 
    speciesId, 
    origBreedName, 
    origSpeciesCode, 
    breedId, 
    birthDate.string, 
    birthDate.date_time, 
    Analyte.timestamp, 
    Analyte.patientId, 
    Analyte.contextId, 
    Analyte.analyteServiceCode as analyteServiceCode, 
    Analyte.analyteLowValue, Analyte.analyteHighValue, 
    Analyte.resultValue
FROM [lustgarten_sandbox.Patient] as Patient
    on Analyte.patientId = Patient.ptID 
where 
    Patient.ptID IS NOT NULL 
    AND Patient.speciesId IS NOT NULL Limit 12000;

如果有人能帮我,因为我似乎找不到错误。让我知道我哪里出错了

让我来美化一下这个问题:

SELECT
  timestamp,
  REGEXP_EXTRACT(Analyte.id,
    r'^Patient,
    (\d+)') AS patientId,
  Analyte.contextId AS contextId,
  Analyte.analyteServiceCode AS analyteServiceCode,
  Analyte.resultRefRangeLow AS analyteLowValue,
  Analyte.resultRefRangeHigh AS analyteHighValue,
  Analyte.resultValue AS resultValue
FROM
  [lustgarten_sandbox.Analyte] AS Analyte
JOIN (
  SELECT
    AnalyteMapping.CONTEXTID AS contextId,
    AnalyteMapping.CODE AS analyteServiceCode,
    AnalyteName.NAME
  FROM
    [lustgarten_sandbox.AnalyteMapping] AS AnalyteMapping
  JOIN
    [lustgarten_sandbox.AnalyteName] AS AnalyteName
  ON
    AnalyteName.id = AnalyteMapping.id
  WHERE
    UPPER(AnalyteName.NAME) = 'Analyte'
    ) AS SpecificAnalyte
ON
  SpecificAnalyte.contextId = Analyte.contextId
  AND SpecificAnalyte.analyteServiceCode = Analyte.analyteServiceCode
JOIN
SELECT
  ldePatientId AS ptID,
  origBreedCode,
  speciesId,
  origBreedName,
  origSpeciesCode,
  breedId,
  birthDate.string,
  birthDate.date_time,
  Analyte.timestamp,
  Analyte.patientId,
  Analyte.contextId,
  Analyte.analyteServiceCode AS analyteServiceCode,
  Analyte.analyteLowValue,
  Analyte.analyteHighValue,
  Analyte.resultValue
FROM
  [lustgarten_sandbox.Patient] AS Patient
ON
  Analyte.patientId = Patient.ptID
WHERE
  Patient.ptID IS NOT NULL
  AND Patient.speciesId IS NOT NULL
LIMIT
  12000;
错误:在第30行第1列遇到“JOIN”“JOIN”。应为:

现在更容易发现问题:连接患者的子查询需要括号:

像这样:

SELECT
  timestamp,
  REGEXP_EXTRACT(Analyte.id,
    r'^Patient,
    (\d+)') AS patientId,
  Analyte.contextId AS contextId,
  Analyte.analyteServiceCode AS analyteServiceCode,
  Analyte.resultRefRangeLow AS analyteLowValue,
  Analyte.resultRefRangeHigh AS analyteHighValue,
  Analyte.resultValue AS resultValue
FROM
  [lustgarten_sandbox.Analyte] AS Analyte
JOIN (
  SELECT
    AnalyteMapping.CONTEXTID AS contextId,
    AnalyteMapping.CODE AS analyteServiceCode,
    AnalyteName.NAME
  FROM
    [lustgarten_sandbox.AnalyteMapping] AS AnalyteMapping
  JOIN
    [lustgarten_sandbox.AnalyteName] AS AnalyteName
  ON
    AnalyteName.id = AnalyteMapping.id
  WHERE
    UPPER(AnalyteName.NAME) = 'Analyte'
    ) AS SpecificAnalyte
ON
  SpecificAnalyte.contextId = Analyte.contextId
  AND SpecificAnalyte.analyteServiceCode = Analyte.analyteServiceCode
JOIN (
  SELECT
    ldePatientId AS ptID,
    origBreedCode,
    speciesId,
    origBreedName,
    origSpeciesCode,
    breedId,
    birthDate.string,
    birthDate.date_time,
    Analyte.timestamp,
    Analyte.patientId,
    Analyte.contextId,
    Analyte.analyteServiceCode AS analyteServiceCode,
    Analyte.analyteLowValue,
    Analyte.analyteHighValue,
    Analyte.resultValue
  FROM
    [lustgarten_sandbox.Patient]) AS Patient
ON
  Analyte.patientId = Patient.ptID
WHERE
  Patient.ptID IS NOT NULL
  AND Patient.speciesId IS NOT NULL
LIMIT
  12000;
现在我们有了“错误:28.1-0.0:一个查询不能有多个联接子句”

剩下的唯一一步是将所述连接移动到子查询中(就像您已经使用“AnalyteName.id=AnalyteMapping.id”所做的那样),以使其工作

例如: (我无法确定数据或表格结构)


让我来美化一下这个问题:

SELECT
  timestamp,
  REGEXP_EXTRACT(Analyte.id,
    r'^Patient,
    (\d+)') AS patientId,
  Analyte.contextId AS contextId,
  Analyte.analyteServiceCode AS analyteServiceCode,
  Analyte.resultRefRangeLow AS analyteLowValue,
  Analyte.resultRefRangeHigh AS analyteHighValue,
  Analyte.resultValue AS resultValue
FROM
  [lustgarten_sandbox.Analyte] AS Analyte
JOIN (
  SELECT
    AnalyteMapping.CONTEXTID AS contextId,
    AnalyteMapping.CODE AS analyteServiceCode,
    AnalyteName.NAME
  FROM
    [lustgarten_sandbox.AnalyteMapping] AS AnalyteMapping
  JOIN
    [lustgarten_sandbox.AnalyteName] AS AnalyteName
  ON
    AnalyteName.id = AnalyteMapping.id
  WHERE
    UPPER(AnalyteName.NAME) = 'Analyte'
    ) AS SpecificAnalyte
ON
  SpecificAnalyte.contextId = Analyte.contextId
  AND SpecificAnalyte.analyteServiceCode = Analyte.analyteServiceCode
JOIN
SELECT
  ldePatientId AS ptID,
  origBreedCode,
  speciesId,
  origBreedName,
  origSpeciesCode,
  breedId,
  birthDate.string,
  birthDate.date_time,
  Analyte.timestamp,
  Analyte.patientId,
  Analyte.contextId,
  Analyte.analyteServiceCode AS analyteServiceCode,
  Analyte.analyteLowValue,
  Analyte.analyteHighValue,
  Analyte.resultValue
FROM
  [lustgarten_sandbox.Patient] AS Patient
ON
  Analyte.patientId = Patient.ptID
WHERE
  Patient.ptID IS NOT NULL
  AND Patient.speciesId IS NOT NULL
LIMIT
  12000;
错误:在第30行第1列遇到“JOIN”“JOIN”。应为:

现在更容易发现问题:连接患者的子查询需要括号:

像这样:

SELECT
  timestamp,
  REGEXP_EXTRACT(Analyte.id,
    r'^Patient,
    (\d+)') AS patientId,
  Analyte.contextId AS contextId,
  Analyte.analyteServiceCode AS analyteServiceCode,
  Analyte.resultRefRangeLow AS analyteLowValue,
  Analyte.resultRefRangeHigh AS analyteHighValue,
  Analyte.resultValue AS resultValue
FROM
  [lustgarten_sandbox.Analyte] AS Analyte
JOIN (
  SELECT
    AnalyteMapping.CONTEXTID AS contextId,
    AnalyteMapping.CODE AS analyteServiceCode,
    AnalyteName.NAME
  FROM
    [lustgarten_sandbox.AnalyteMapping] AS AnalyteMapping
  JOIN
    [lustgarten_sandbox.AnalyteName] AS AnalyteName
  ON
    AnalyteName.id = AnalyteMapping.id
  WHERE
    UPPER(AnalyteName.NAME) = 'Analyte'
    ) AS SpecificAnalyte
ON
  SpecificAnalyte.contextId = Analyte.contextId
  AND SpecificAnalyte.analyteServiceCode = Analyte.analyteServiceCode
JOIN (
  SELECT
    ldePatientId AS ptID,
    origBreedCode,
    speciesId,
    origBreedName,
    origSpeciesCode,
    breedId,
    birthDate.string,
    birthDate.date_time,
    Analyte.timestamp,
    Analyte.patientId,
    Analyte.contextId,
    Analyte.analyteServiceCode AS analyteServiceCode,
    Analyte.analyteLowValue,
    Analyte.analyteHighValue,
    Analyte.resultValue
  FROM
    [lustgarten_sandbox.Patient]) AS Patient
ON
  Analyte.patientId = Patient.ptID
WHERE
  Patient.ptID IS NOT NULL
  AND Patient.speciesId IS NOT NULL
LIMIT
  12000;
现在我们有了“错误:28.1-0.0:一个查询不能有多个联接子句”

剩下的唯一一步是将所述连接移动到子查询中(就像您已经使用“AnalyteName.id=AnalyteMapping.id”所做的那样),以使其工作

例如: (我无法确定数据或表格结构)


你错过了几篇偏执的论文。在代码中查找我的注释:

SELECT
    timestamp,
    REGEXP_EXTRACT(Analyte.id,r'^Patient, (\d+)') as patientId,
    Analyte.contextId as contextId, 
    Analyte.analyteServiceCode as analyteServiceCode,
    Analyte.resultRefRangeLow as analyteLowValue,
    Analyte.resultRefRangeHigh as analyteHighValue, 
    Analyte.resultValue as resultValue
FROM
    [lustgarten_sandbox.Analyte] as Analyte 
JOIN
    (
    SELECT
        AnalyteMapping.CONTEXTID as contextId,
        AnalyteMapping.CODE as analyteServiceCode,
        AnalyteName.NAME
    FROM
        [lustgarten_sandbox.AnalyteMapping] as AnalyteMapping
    JOIN
        [lustgarten_sandbox.AnalyteName] as AnalyteName
        ON AnalyteName.id = AnalyteMapping.id
    WHERE
        upper(AnalyteName.NAME) = 'Analyte'
    ) as SpecificAnalyte 
    ON SpecificAnalyte.contextId = Analyte.contextId
    AND SpecificAnalyte.analyteServiceCode = Analyte.analyteServiceCode
JOIN
    ( -- Added This
    SELECT
        ldePatientId as ptID,
        origBreedCode,
        speciesId,
        origBreedName,
        origSpeciesCode,
        breedId,
        birthDate.string,
        birthDate.date_time,
        Analyte.timestamp, 
        Analyte.patientId,
        Analyte.contextId,
        Analyte.analyteServiceCode as analyteServiceCode,
        Analyte.analyteLowValue,
        Analyte.analyteHighValue,
        Analyte.resultValue
    FROM
        [lustgarten_sandbox.Patient] as Patient
        ON Analyte.patientId = Patient.ptID
    WHERE
        Patient.ptID IS NOT NULL AND
        Patient.speciesId IS NOT NULL
    ) Patient -- Added This
LIMIT 12000;

你错过了几篇偏执的论文。在代码中查找我的注释:

SELECT
    timestamp,
    REGEXP_EXTRACT(Analyte.id,r'^Patient, (\d+)') as patientId,
    Analyte.contextId as contextId, 
    Analyte.analyteServiceCode as analyteServiceCode,
    Analyte.resultRefRangeLow as analyteLowValue,
    Analyte.resultRefRangeHigh as analyteHighValue, 
    Analyte.resultValue as resultValue
FROM
    [lustgarten_sandbox.Analyte] as Analyte 
JOIN
    (
    SELECT
        AnalyteMapping.CONTEXTID as contextId,
        AnalyteMapping.CODE as analyteServiceCode,
        AnalyteName.NAME
    FROM
        [lustgarten_sandbox.AnalyteMapping] as AnalyteMapping
    JOIN
        [lustgarten_sandbox.AnalyteName] as AnalyteName
        ON AnalyteName.id = AnalyteMapping.id
    WHERE
        upper(AnalyteName.NAME) = 'Analyte'
    ) as SpecificAnalyte 
    ON SpecificAnalyte.contextId = Analyte.contextId
    AND SpecificAnalyte.analyteServiceCode = Analyte.analyteServiceCode
JOIN
    ( -- Added This
    SELECT
        ldePatientId as ptID,
        origBreedCode,
        speciesId,
        origBreedName,
        origSpeciesCode,
        breedId,
        birthDate.string,
        birthDate.date_time,
        Analyte.timestamp, 
        Analyte.patientId,
        Analyte.contextId,
        Analyte.analyteServiceCode as analyteServiceCode,
        Analyte.analyteLowValue,
        Analyte.analyteHighValue,
        Analyte.resultValue
    FROM
        [lustgarten_sandbox.Patient] as Patient
        ON Analyte.patientId = Patient.ptID
    WHERE
        Patient.ptID IS NOT NULL AND
        Patient.speciesId IS NOT NULL
    ) Patient -- Added This
LIMIT 12000;
正确的格式(即缩进)可能会帮助您发现缺少的括号更容易…正确的格式(即缩进)可能会帮助您发现缺少的括号更容易。。。