【问题标题】:Postgres to Snowflake json syntaxPostgres 到 Snowflake json 语法
【发布时间】:2021-05-03 14:25:06
【问题描述】:

在构建 ETL 模型时,我正在寻求使用 Json 语法从 Postgres 迁移到 SnowFlake 的帮助

select 

n.Object->c.name->>'some key in json file' as code

from names n
left join country c

你会如何在 Snowflake dbt 中写这个??

——更新: 在这里我会尝试更好地解释,因为这涉及到 3 个表

1- 公司 2- 国家 3-公司评级

为了方便起见,我们可以分别调用列 A、B、C

公司表中的

json列:

{
  "AU": {},
  "CA": {},
  "GB": {
    "company rating": "ijbfgp"
  },
  "US": {},
  "ES": {
    "company rating": "piayerb",
  },
}

国家/地区表

国家/地区名称/代码列,所有国家/地区为 TEXT

公司评级表

技术名称列,用于解释该评级为 TEXT 的内容

我想做的是像在 postgres 中一样查询某事

company.A -> country.B ->> company.country_rating

【问题讨论】:

  • 你能分享一个json的样本吗?
  • 和预期的输出?
  • @FelipeHoffa,我更新了输入,如果你们可以看看,将不胜感激
  • @MikeWalton 已编辑,很想听听您的想法

标签: sql etl snowflake-cloud-data-platform dbt


【解决方案1】:

这是我可以根据您的描述重建的最好的:

with company as (
    select parse_json('    
        {
      "AU": {},
      "CA": {},
      "GB": {
        "company rating": "ijbfgp"
      },
      "US": {},
      "ES": {
        "company rating": "piayerb",
      },
    }') x
), country as (
    select 'GB' country_code, 'Great Britain' country_name
    union all select 'ES', 'Spain'
), ratings as (
    select 'ijbfgp' rating_code, 'incredible just before forgetting goal posts' rating
    union all select 'piayerb', 'praying into abiss you exit running beach'
)

select country_name, rating
from company, country, ratings
where ratings.rating_code=get(x, country_code):"company rating"

在这种情况下导航 JSON 的关键是 get(x, country_code):"company rating"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多