英文:
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: "CREATE OR REPLACE TEMPORARY TABLE TEMP_TABLE AS " + SQL_QUERY});
var rs = stmt.execute();
// Get the result set metadata
var stmt = snowflake.createStatement({sqlText: "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = 'TEMP_TABLE'"});
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: "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);
}
}
}
// Build a new SQL query that only includes non-empty columns
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;
$$
Use the above stored procedure, and call it like this, where the parameter is an SQL Query:
CALL remove_empty_columns('SELECT * FROM DB_DW_PROD.SC_ODS.ODS_SUBSCR_INFO')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论