BigQuery-根据条件替换空值分派

时间:2020-01-23 06:43:55

标签: google-bigquery

我有2个表1&2。在table1内,我创建了一个派生字段“ actualoutput”,通过该字段我将其与以“ newgroup”为连接器的table-2结合起来,以获得字段“ finalgroup”。

现在在输出表中,字段“ finalgroup”具有NULL值,以蓝色突出显示。

我想要的是-仅当“ finalgroup”中的值为NULL时,我才要根据表1中存在的“网站”字段替换为条件,并创建新列“ finalgroup2”。因此,如果存在“网站”,则finalgroup2的值为“ web”,否则为“离线”(以红色突出显示),其他所有值均保留“ finalgroup”的值。

但是我没有得到逻辑(尝试多次尝试)以获取输出,如图像-FinalTable。如果可以直接在“ finalgroup”字段中获得此逻辑,那么我就可以了,即不需要“ finalgroup2”

有人可以帮忙吗?

参考图片:

enter image description here

我使用的查询:

SELECT
  source,
  detail,
  CONCAT(IFNULL(Source,
      ''), "-", IFNULL(Detail,
      '')) AS actualoutput,
  website,
  newgroup,
  finalgroup,
  CASE
    WHEN finalgroup IS NULL AND website IS NOT NULL THEN "web"
    WHEN finalgroup IS NULL
  AND website IS NULL THEN "offline"
  ELSE
  "finalgroup"
END
  AS finalgroup2
FROM (
  SELECT
    source,
    detail,
    CONCAT(IFNULL(Source,
        ''), "-", IFNULL(Detail,
        '')) AS actualoutput,
    website,
    newgroup,
    finalgroup
  FROM (
    SELECT
      source,
      detail,
      CONCAT(IFNULL(Source,
          ''), "-", IFNULL(Detail,
          '')) AS actualoutput,
      website
    FROM (
      SELECT
        source,
        detail,
        website
      FROM
        `dataset.concat`))
  LEFT JOIN (
    SELECT
      newgroup,
      finalgroup
    FROM
      `dataset.finalgroup`)
  ON
    actualoutput=newgroup)

1 个答案:

答案 0 :(得分:1)

尝试这样做:

WITH
  tnew AS(
  SELECT
    source,
    detail,
    CONCAT(IFNULL(Source,
        ''), "-", IFNULL(Detail,
        '')) AS actualoutput,
    website
  FROM
    table1 )
SELECT
  source,
  detail,
  website,
  actualoutput,
  finalgroup,
  CASE
    WHEN finalgroup IS NOT NULL THEN finalgroup
    WHEN finalgroup IS NULL
  AND website IS NOT NULL THEN "web"
    WHEN finalgroup IS NULL AND website IS NULL THEN "offline"
END
  finalgroup2
FROM
  tnew
LEFT JOIN
  table2
ON
  actualoutput = newgroup

请告诉我是否对您有帮助