雪花存储过程动态列数据透视

时间:2021-02-09 07:02:08

标签: stored-procedures snowflake-cloud-data-platform

我正在尝试在 Snowflake 中创建一个存储过程,该过程将数据透视到派生表中。枢轴列本质上是动态的。我找到了一种通过参数传递来做到这一点的方法。我正在尝试在不传递任何参数的情况下执行相同操作,但代码不起作用。

我的方法是在表上应用一个循环,从中我将列提取到一个变量中。然后在枢轴中传递这个变量。 while 循环本身似乎工作正常。将此变量传递给数据透视表时会出现错误。

我使用的代码:

CREATE OR REPLACE PROCEDURE LOOP_EXMPL_5()
returns  varchar
language javascript
as
$$ 
var column1 = 'qwerty';
var command = `SELECT ATTR_NAME FROM TBL_DIM`;
var stmt = snowflake.createStatement({sqlText: command});
var rs = stmt.execute();
while (rs.next()) 
   {
     var column1 = column1.concat(",","'",rs.getColumnValue(1),"'"); 
   }
var column2 = column1
var command_1 = `CREATE OR REPLACE VIEW HIERARCHY_VIEW_2 AS SELECT * FROM (SELECT MSTR.PROD_CODE AS 
PROD_CODE,DIM.ATTR_NAME AS ATTR_NAME,MSTR.ATTR_VALUE AS ATTR_VALUE FROM TBL_DIM DIM INNER JOIN 
TBL_MSTR MSTR ON DIM.ATTR_KEY=MSTR.ATTR_KEY ) Q
              PIVOT (MAX (Q.ATTR_VALUE) FOR Q.ATTR_NAME IN ('${column2}'))
              AS P
              ORDER BY P.PROD_CODE;`;
 var stmt_1 = snowflake.createStatement({sqlText: command_1});
 var rs_1 = stmt_1.execute();
 return 'success'
 $$;   

我得到的错误:

存储过程 LOOP_EXMPL_5 中的执行错误:SQL 编译错误:语法错误第 2 行在位置 73 意外的“区域”。在 Statement.execute,第 16 行,位置 21。

正在传递的变量值:

qwerty,'Region','Sub-Region','Division','Company-Product','Company-Mfg','Company-Ship From to Customer','Business Unit','Category',' Sub-Category','Segment','Sub-Segment','Brand','Aggregate Brand','Sub-Brand','PPG'

我会以某种方式删除 SQL 中的 qwerty 部分。

3 个答案:

答案 0 :(得分:2)

这是工作代码。非常感谢 Felipe 和 Greg 的帮助。

CREATE OR REPLACE PROCEDURE LOOP_EXMPL_9()
returns  varchar
language javascript
as
$$ 
  var column1 = "";
  var command = `SELECT ATTR_NAME FROM TBL_DIM`;
  var stmt = snowflake.createStatement({sqlText: command});
  var rs = stmt.execute();
  while (rs.next()) 
   {
     if (column1 != "") column1 += ",";
     column1 += `'${rs.getColumnValue (1)}'`;
   }
  var column2 = column1;
  var command_1 = `CREATE OR REPLACE VIEW HIERARCHY_VIEW_2 AS SELECT * FROM (SELECT 
  MSTR.PROD_CODE AS PROD_CODE,DIM.ATTR_NAME AS ATTR_NAME,MSTR.ATTR_VALUE AS ATTR_VALUE 
  FROM TBL_DIM DIM INNER JOIN TBL_MSTR MSTR ON DIM.ATTR_KEY=MSTR.ATTR_KEY ) Q
              PIVOT (MAX (Q.ATTR_VALUE) FOR Q.ATTR_NAME IN (${column2}))
              AS P
              ORDER BY P.PROD_CODE;`;
  var stmt_1 = snowflake.createStatement({sqlText: command_1});
  var rs_1 = stmt_1.execute();
  return 'success'   
$$;   

答案 1 :(得分:1)

有关您的目标,请参阅:

要调试当前问题:column1.concat(",","'",rs.getColumnValue(1),"'"); 没有正确转义和引用列名。

设置 UDF 以返回该值,然后是 var command_1 的值,以便您可以进行适当的调试。

逃避列的好方法:

select '\\'' 
        || listagg(distinct pivot_column, '\\',\\'') within group (order by pivot_column)
        || '\\'' 

然后使用:

for pivot_column in (${col_list}))

答案 2 :(得分:1)

您可以从一个空字符串开始:

var column1 = "";

然后您可以像这样连接列列表:

if (column1 != "") column1 += ",";
column1 += `"${rs.getColumnValue(1)}"`);

您收到 SQL 语法错误的原因是列名应该用双引号括起来,而用单引号括起来。