在重复记录中添加新元素时,将空记录保持在BigQuery中

发布于 2025-02-09 20:17:15 字数 1530 浏览 3 评论 0原文

我在BigQuery中有一个类型重复记录的字段,该字段存储了此JSON:

  "users": [{
    "key": "name",
    "value": {
      "string_value": foo,
      "int_value": null,
      "float_value": null,
      "double_value": null
    }
  }, {
    "key": "age",
    "value": {
      "string_value": null,
      "int_value": "477",
      "float_value": null,
      "double_value": null
    }
  }

我使用Unnest查询这些字段,就像这样:

  select * from myTable t,
  unnest(users) as name, 
  unnest(users) as age, 

  WHERE name.key = 'name' 
  AND age.key = 'age'

我的问题是我添加了第三个字段,

  "users": [{
    "key": "name",
    "value": {
      "string_value": foo,
      "int_value": null,
      "float_value": null,
      "double_value": null
    }
  }, {
    "key": "age",
    "value": {
      "string_value": null,
      "int_value": "477",
      "float_value": null,
      "double_value": null
    }
  },
  {
    "key": "lastName",
    "value": {
      "string_value": Johnson,
      "int_value": null,
      "float_value": null,
      "double_value": null
    }
  }

以前不存在姓氏“现在仅包含以前的两个字段的行现在被排除在我的查询之外,甚至使用左联接。

  select * from myTable t,
  unnest(users) as name, 
  unnest(users) as age
  left join unnest (users) as lastName 

  WHERE name.key = 'name' 
  AND age.key = 'age'
  AND lastName.key = 'lastName'

这仅返回包含3个字段的行(甚至是左联接),在将新元素添加到重复字段之前,我缺少所有数据。 我如何保持空行?

要澄清,我的查询应该返回这样的东西(现在我没有得到第一个):

  John, 12, null
  Juan, 14, Perez

I have a field of type repeated record in bigquery that stores this json:

  "users": [{
    "key": "name",
    "value": {
      "string_value": foo,
      "int_value": null,
      "float_value": null,
      "double_value": null
    }
  }, {
    "key": "age",
    "value": {
      "string_value": null,
      "int_value": "477",
      "float_value": null,
      "double_value": null
    }
  }

I query those field using UNNEST, just like this:

  select * from myTable t,
  unnest(users) as name, 
  unnest(users) as age, 

  WHERE name.key = 'name' 
  AND age.key = 'age'

My problem is that I have added a third field

  "users": [{
    "key": "name",
    "value": {
      "string_value": foo,
      "int_value": null,
      "float_value": null,
      "double_value": null
    }
  }, {
    "key": "age",
    "value": {
      "string_value": null,
      "int_value": "477",
      "float_value": null,
      "double_value": null
    }
  },
  {
    "key": "lastName",
    "value": {
      "string_value": Johnson,
      "int_value": null,
      "float_value": null,
      "double_value": null
    }
  }

The last name field didn't exist before so I have "historical" rows that contained only the 2 previous fields that are being excluded from my query now, even using a left join.

  select * from myTable t,
  unnest(users) as name, 
  unnest(users) as age
  left join unnest (users) as lastName 

  WHERE name.key = 'name' 
  AND age.key = 'age'
  AND lastName.key = 'lastName'

this ONLY returns rows that contain the 3 fields (even tho is a left join) and I'm missing all the data from before I added the new element to the repeated field.
How can I keep null rows?

To clarify, my query should return something like this (right now I'm not getting the first one):

  John, 12, null
  Juan, 14, Perez

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2025-02-16 20:17:15

我想您正在查询bq中的firebase数据。

首先,您不应为用户结构中的每个字段添加一个新的加入。
相反,您应该在数组中搜索键,然后在Value struct中获取相关字段。

  select 
    (select value.string_value from unnest(users) where key = 'name') as user,
    (select value.int_value from unnest(users) where key = 'age') as age,
    (select value.string_value from unnest(users) where key = 'lastName') as lastName,
  from myTable 

上面,我为名称和姓氏使用字符串值,以及年龄字段的int值。请调整这些数据。

I guess you're querying Firebase data in BQ.

Firstly, you shouldn't add a new join for each field in user struct.
Instead, you should search for the key in the array and then get the relevant field in value struct.

  select 
    (select value.string_value from unnest(users) where key = 'name') as user,
    (select value.int_value from unnest(users) where key = 'age') as age,
    (select value.string_value from unnest(users) where key = 'lastName') as lastName,
  from myTable 

Above, I used string value for name and lastname, and int value for age fields. Please adjust these for your data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文