Snowflake 中是否有一种方法可以仅获取表中具有值的列?

huangapple go评论54阅读模式
英文:

Is there a way in Snowflake that I can get only the columns in a table that have values?

问题

只返回具有数值的列。

英文:

Some tables are very wide but many of the columns are just empty. I'm looking for something to return me only the columns that have values.

答案1

得分: 0

CREATE OR REPLACE PROCEDURE remove_empty_columns(sql_query VARCHAR)
RETURNS STRING NOT NULL
LANGUAGE javascript
execute as owner
AS
$$
var stmt = snowflake.createStatement({sqlText: "CREATE OR REPLACE TEMPORARY TABLE TEMP_TABLE AS " + SQL_QUERY});
var rs = stmt.execute();

// 获取结果集元数据
var stmt = snowflake.createStatement({sqlText: "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = 'TEMP_TABLE'"});
var rs = stmt.execute();

// 初始化一个数组以存储非空列名
var non_empty_cols = [];

// 循环遍历结果集并检查非空列
while (rs.next()) {
var col_value = rs.getColumnValue(1);
var stmt = snowflake.createStatement({sqlText: "SELECT distinct " + col_value + " FROM TEMP_TABLE"});
var nr_values = stmt.execute();
if (nr_values.next()) {
if (nr_values.getColumnValue(1) !== null || nr_values.getRowCount() > 1) {
non_empty_cols.push(col_value);
}
}
}

// 构建一个新的SQL查询,只包括非空列
var new_query = "SELECT ";
for (var i = 0; i < non_empty_cols.length; i++) {
new_query += non_empty_cols[i];
if (i < non_empty_cols.length - 1) {
new_query += ", ";
}
}
new_query += " FROM (" + SQL_QUERY + ") t";

return new_query;
$$

使用上述存储过程,并像这样调用它,其中参数是一个SQL查询:

CALL remove_empty_columns('SELECT * FROM DB_DW_PROD.SC_ODS.ODS_SUBSCR_INFO')

英文:
CREATE OR REPLACE PROCEDURE remove_empty_columns(sql_query VARCHAR)
  RETURNS STRING NOT NULL
  LANGUAGE javascript
  execute as owner
  AS
  $$
  var stmt = snowflake.createStatement({sqlText: &quot;CREATE OR REPLACE TEMPORARY TABLE TEMP_TABLE AS &quot; + SQL_QUERY});
  var rs = stmt.execute();
  
  // Get the result set metadata
 var stmt = snowflake.createStatement({sqlText: &quot;SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = &#39;TEMP_TABLE&#39;&quot;});
  var rs = stmt.execute();
 
  // Initialize an array to store the non-empty column names
  var non_empty_cols = [];
  
  // Loop through the result set and check for non-empty columns
  while (rs.next()) {
      var col_value = rs.getColumnValue(1);
      var stmt = snowflake.createStatement({sqlText: &quot;SELECT distinct &quot; + col_value + &quot; FROM TEMP_TABLE&quot;});
  	  var nr_values = stmt.execute();
  	  if (nr_values.next()) {
      if (nr_values.getColumnValue(1) !== null || nr_values.getRowCount() &gt; 1) {
        non_empty_cols.push(col_value);
      }
    }
  }

  // Build a new SQL query that only includes non-empty columns
  var new_query = &quot;SELECT &quot;;
  for (var i = 0; i &lt; non_empty_cols.length; i++) {
    new_query += non_empty_cols[i];
    if (i &lt; non_empty_cols.length - 1) {
      new_query += &quot;, &quot;;
    }
  }
  new_query += &quot; FROM (&quot; + SQL_QUERY + &quot;) t&quot;;

  return new_query;
$$

Use the above stored procedure, and call it like this, where the parameter is an SQL Query:

CALL remove_empty_columns(&#39;SELECT * FROM DB_DW_PROD.SC_ODS.ODS_SUBSCR_INFO&#39;)

huangapple
  • 本文由 发表于 2023年3月12日 09:23:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定