简体   繁体   English

使用 SQL 将 output 从视图转换为 json 和雪花数组的问题

[英]issues converting output from a view to json and array in snowflake using SQL

I am new to snowflake and have successfully created a view from a table using sql, but I am having issues creating a view that transforms the whole table into Json and array我是雪花的新手,并且已经使用 sql 从表中成功创建了一个视图,但是在创建将整个表转换为 Json 和数组的视图时遇到问题

my view我的观点

create or replace  view my_view as (
  select id, town, created_date, updated_at, array_construct( 
 object_construct('service','green','period', 
 object_construct('Type',type,'end_date', end_date)))Services
from demo
);

my_view output my_view output

id ID town created_date创建日期 updated_at更新时间 Services服务
123 123 modak莫达克 2024-03-29 2024-03-29 2024-03-29 2024-03-29 [{ "service": "green", "period":{"Type": "definite", "end_date": "2024-03-29 11:17:42.000"}}] [{“服务”:“绿色”,“周期”:{“类型”:“确定”,“结束日期”:“2024-03-29 11:17:42.000”}}]

my objective is to create two views from my_view that will do the following:我的目标是从 my_view 创建两个视图,它们将执行以下操作:

  1. convert my_view to json将 my_view 转换为 json
  2. Convert my_view to array将 my_view 转换为数组

The below codes are able to convert my_view to json and array successfully以下代码能够将 my_view 转换为 json 并成功排列

select array_agg(object_construct(*)) from my_view; 

select object_construct(*) from my_view; 

But when I try to create a view with it I get errors但是当我尝试用它创建一个视图时,我得到了错误

create or replace  view my_json as (
  select object_construct(*) from my_view
);

error generated产生的错误

SQL compilation error: Missing column specification

The expression has to be aliased:表达式必须有别名:

create or replace  view my_json as (
  select object_construct(*) AS output from my_view
);

or:或者:

create or replace view my_json(output) as (
  select object_construct(*) from my_view
);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM