简体   繁体   English

使用存储过程重新组织数据列雪花

[英]reorganizing data columns Snowflake using stored procedure

Using stored procdeure in Snowflake I am trying to change the columns order for a part of the data table.在雪花中使用存储过程我正在尝试更改部分数据表的列顺序。

Here is my table这是我的桌子

column1 column2 column3 column4    column5
---------------------------------------------
Year   Country  Name    City
2020    US      Briand   NY
2021    US      John     LA
2021    UK      Mark     London

City   Name    Year      Country      Age
Paris  Jacques  2017      FR          55
Madrid Juan     2015      ES          25
Dublin Steven   2018      IE          37

So there is 5 differents structures in the same table.所以同一张表中有5个不同的结构。 All 5 structures doesn't have the same number of columns but the column name can be match and missing column can be fill with null values.所有 5 个结构的列数不同,但列名可以匹配,缺失的列可以用 null 值填充。 Moreover there is a blank row between each different data structure.此外,每个不同的数据结构之间都有一个空白行。 The output should be: output 应该是:

Year      Country     City     Name          Age
-------------------------------------------------
2020        US        NY        Briand       Null
2021        US        LA        John         Null
2021        UK        London    Mark         Null
2017        FR        Paris     Jacques      55
2015        ES        Madrid    Juan         25
2018        IE        Dublin    Steven       37

So I thought that the only way is to do a stored procedure but I have no idea how it works and if it is possible to resolve my problem this way.所以我认为唯一的方法是做一个存储过程,但我不知道它是如何工作的,以及是否可以通过这种方式解决我的问题。

CREATE OR REPLACE PROCEDURE proc_columns_matching()
returns string
language javascript
as
$$
var sql = "select * from countries_pp";
var statement1 = snowflake.createStatement( {sqlText: pp} );
var result_set1 = statement1.execute();

while(result_set1.next() != ''){
var column1= result_set1.getColumnValue(1)
}

return column1;
$$
;

CALL proc_smart_impulse();

So I tried to identify the blank cell to split the table but I am stuck here.所以我试图识别空白单元格来拆分表格,但我被困在这里。

I believe you cannot re-order columns using ALTER commands and the recommended solution is to use a view.我相信您不能使用 ALTER 命令对列重新排序,推荐的解决方案是使用视图。 For your case it would probably require using UNION to cater for the different structures对于您的情况,可能需要使用 UNION 来满足不同的结构

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

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