首页 > 解决方案 > SQL中所有变量与目标变量的相关性

问题描述

我有一张有 270 列的表。我选择了其中一个作为目标变量,我想编写一个查询,分别与目标列查找每个变量的相关性。有没有办法在不硬编码 corr 函数中的列名的情况下做到这一点?我正在使用 Oracle SQL。

标签: sqloraclecorrelation

解决方案


并不真地。但在 Oracle 12+ 中,您可能会发现这更简单:

select x.which, corr(x.target, x.source)
from t cross join lateral
     (select 'source001' as which, target, source1 as source from dual union all
      select 'source002' as which, target, source2 as source from dual union all
      . . .
      select 'source270' as which, target, source270 as source from dual
     ) x
group by which;

对于相关性,并不比单独列出corr()函数更简单。但是您可以轻松地添加更多统计信息——比如 、min()max()——count()而无需重复所有列(或遇到列限制)。


推荐阅读