[英]List columns of table on which there is at least one row with a non null value for a specific query
I'm trying to find the proper query to : 我试图找到合适的查询:
Get all the column names of a table on which there is at least one row with a non null value for a specific query.
获取表的所有列名,该表至少有一行针对特定查询具有非空值。
Meaning : I will see which columns have at least one value set in the record returned by my given query. 含义:我将查看给定查询返回的记录中哪些列设置了至少一个值。
I hope I'm clear enough. 我希望我足够清楚。
I think you need something as the following: 我认为您需要以下物品:
SELECT CASE WHEN MAX(col1) IS NOT NULL THEN 'COL1' END ||','
|| CASE WHEN MAX(col2) IS NOT NULL THEN 'COL2' END ||','
...
FROM T
Then use REGEXP_REPLACE to replace duplicated ,
. 然后使用REGEXP_REPLACE来代替重复的
,
。 You can use user_tab_columns
to generate this query dynamically as mentioned. 您可以使用
user_tab_columns
如前所述动态生成此查询。
Please check below anonymous block for your case , tablename should be given at 3 places in below query 请检查下面的匿名块是否适合您的情况,表名应在以下查询的3个地方给出
Declare
v_columnlist varchar2(32627);
v_noofcolumns number;
Columnnm varchar2(1000);
Finalcolumns varchar2(32627);
v_count number:=0;
plsql_block varchar2(32627);
Begin
select LISTAGG(column_name,',') within group (order by column_id),max(column_id)
into v_columnlist , v_noofcolumns
from user_tab_columns
where table_name='tablename';
FOR Lcntr IN 1..v_noofcolumns
LOOP
select REGEXP_SUBSTR(v_columnlist,'[^,]+',1,Lcntr)
into Columnnm
from dual;
plsql_block := 'select count(*) from tablename where '|| Columnnm || ' is not null ';
EXECUTE IMMEDIATE plsql_block into v_count;
IF v_count > 1 THEN
Finalcolumns:= LTRIM(Finalcolumns ||','||Columnnm,',');
END IF;
END LOOP;
plsql_block := 'select ' || Finalcolumns ||' from tablename ';
DBMS_OUTPUT.PUT_LINE(plsql_block);
END;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.