如何从表中选择具有非空值的列?
我有一个包含数百列的表,其中许多列为空,并且我希望有我的 select 语句,以便只返回那些包含值的列。这将帮助我更好地分析数据。类似于:
从表名中选择(非空列);
我想选择至少具有一个非空值的所有列。
这可以做到吗?
I have a table containing hundreds of columns many of which are null, and I would like have my select statement so that only those columns containing a value are returned. It would help me analyze data better. Something like:
Select (non null columns) from tablename;
I want to select all columns which have at least one non-null value.
Can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看看统计信息,它可能对您有用:
例如您可以检查 NUM_NULLS = NUM_ROWS 是否识别“空”列。
参考:ALL_TAB_COLUMNS,ALL_TABLES。
Have a look as statistics information, it may be useful for you:
For example you can check if NUM_NULLS = NUM_ROWS to identify "empty" columns.
Reference: ALL_TAB_COLUMNS, ALL_TABLES.
使用以下内容:
Table_Name
必须相应地替换...Use the below:
Table_Name
has to be replaced accordingly...这是获取非空列的简单代码。
Here is simple code to get non null columns..
我认为这不能在单个查询中完成。您可能需要一些 plsql 来首先测试哪些列包含数据,并根据该信息组合一个语句。当然,如果表中的数据发生更改,您必须重新创建语句。
I don't think this can be done in a single query. You may need some plsql to first test what columns contain data and put together a statement based on that information. Of course, if the data in your table changes you have to recreate the statement.
该块确定表中的所有列,在动态 SQL 中循环遍历它们并检查它们是否为空,然后构造非空查询的 DBMS 输出查询。
您所要做的就是运行返回的查询。
我已经排除了 PK 和 BLOB 列。
显然,这对于一一列地遍历来说非常慢,并且对于非常热的表来说并不是很好,因为数据可能变化太快,但这对我来说很有效,因为我控制了开发环境中的流量。
This block determines all columns in the table, loops through them in dynamic SQL and checks if they are null, then constructs a DBMS output query of the non-null query.
All you have to do is run the returned query.
I've included the exclusion of PKs and BLOB columns.
Obviously, this is quite slow as going through columns one by one, and it's not going to be great for very hot tables, as data may change too quickly, but this works for me as I control traffic in dev env.
您要求做的是建立对整个结果中每一行的依赖关系。事实上,这并不是您想要的。试想一下,如果在一行中每一列的值为“0”,结果集的架构突然增长到包含所有以前的“空”列,会产生什么后果。您实际上正在以指数方式增加“*”的坏处,现在您的结果集不仅仅依赖于表的元数据 - 但您的整个结果集依赖于纯数据。
你要做的只是选择有你想要的字段,而不是偏离这个简单的计划。
What you're asking to do is establish a dependency on each row in the whole result. This is in fact not ever what you want. Just think of the ramifications if in one row every column had a value of '0' -- suddenly the schema of your result set grows to include all of those previously "empty" columns. You're effectively growing the badness of '*' exponentially, now your result set is not dependent on just the table's meta-data -- but your whole result set is dependent on the plain data.
What you want to do is just select the fields that have what you want, and not deviate from this simple plan.