简体   繁体   English

我正在尝试使用 python sql 访问存储在雪花表中的数据。 下面是我要访问的下面给出的列

[英]I am trying to access the data stored in a snowflake table using python sql. Below is the columns given below i want to access

Below is the data-sample and i want to access columns value,start.下面是数据样本,我想访问列值,开始。 This data i dumped in one column(DN) of a table (stg)我将这些数据转储到表(stg)的一列(DN)中

{
    "ok": true, 
    "metrics": [
        {
            "name": "t_in", 
            "data": [{"value": 0, "group": {"start": "00:00"}}]
        }, 
        {
            "name": "t_out", 
            "data": [{"value": 0,"group": {"start": "00:00"}}]
        }
    ]
}

##consider many lines stored in same column in different rows. ##考虑存储在不同行的同一列中的多行。

Below query only fetched data for name.下面的查询仅获取名称的数据。 I want to access other columns value also.我也想访问其他列的值。 This query is a part of python script.此查询是 python 脚本的一部分。

         select
            replace(DN : metrics[0].name , '"' , '')as  metrics_name, #able to get
            replace(DN : metrics[2].data , '"' , '')as  metrics_data_value,##suggestion needed          
            replace(DN : metrics.data.start, '"','') as metrics_start, ##suggestion needed
            replace(DN : metrics.data.group.finish, '"','') as metrics_finish, ##suggestion needed

    from stg

Do i need to iterate over data and group?我需要迭代数据和组吗? If yes, please suggest the code.如果是,请建议代码。

Here is an example of how to query that data.以下是如何查询该数据的示例。

Set up sample data:设置样本数据:

create or replace transient table test_db.public.stg (DN variant);
insert overwrite into test_db.public.stg (DN) 
    select parse_json('{
    "ok": true, 
    "metrics": [
        {
            "name": "t_in", 
            "data": [
                {"value": 0, "group": {"start": "00:00"}}
            ]
        }, 
        {
            "name": "t_out", 
            "data": [
                {"value": 0,"group": {"start": "00:00"}}
            ]
        }
    ]
    }');

Select statement example: Select 语句示例:

select 
    DN:metrics[0].name::STRING,
    DN:metrics[1].data,
    DN:metrics[1].data[0].group.start::TIME,
    DN:metrics[1].data[0].group.finish::TIME
from test_db.public.stg;

Instead of querying individual indexes of the JSON arrays, I think you'll want to use the flatten function which is documented here .而不是查询 JSON arrays 的单个索引,我想你会想要使用 flatten function 记录在这里

Here is how you do it with the flatten which is what I am guessing you want:这是我猜你想要的扁平化的方法:

select 
    mtr.value:name::string,
    dta.value,
    dta.value:group.start::string,
    dta.value:group.finish::string
from test_db.public.stg stg,
   lateral flatten(input => stg.DN:metrics) mtr,
   lateral flatten(input => mtr.value:data) dta

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试通过 sql 访问雪花中的以下数据,但在列中获取 NULL 。 请调查一下 - I am trying to access below data in snowflake through sql but getting NULL in the columns. Please look into it 我想在下面存储的 PROC 中参数化 TABLE_TO_BE_DELETED 表 - I want to parameterize TABLE_TO_BE_DELETED table in below stored PROC 我是使用 mysql 的新手-当我尝试创建表时,出现以下错误 - I am new to using mysql - as i was trying to create a table i got the error below 我正在尝试创建一个查询,以将数据从一个表带到另一张表,如下所述? - I am trying to create a query to bring data from one table to another table as described below? 我想对以下给定日期使用正确的日期函数,该函数在下面的Oracle SQL查询中进行了硬编码 - I want to use the proper date function for the below given date which is hard coded in below Oracle SQL query 如何获得累积数据? 我试图在 SQL 中获得以下分桶,但没有得到想要的结果/pyspark - how to get a cumulative data? I am trying to get the below bucketing in SQL, but not getting desired result/pyspark 我一直在尝试编写下面的SQL查询并获得查询下面给出的结果 - I have been trying to write a below SQL query ande getting result as given below the query 我正在尝试针对以下情况编写查询,如下所示将一个表分为2个 - I am trying to write a query for below scenario splitting one table into 2 as below 我正在尝试使用 VBA 中的 SQL SELECT 方法从访问数据库导入数据 - I am trying to import data from an access database using SQL SELECT method in VBA 基于sql我有一个如下表我想在我想删除记录的地方插入一条记录? - based on sql i had a table as below i want to insert a record where i want to delete a record?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM